You can use os.kill():

os.kill(os.getpid(), signal.SIGUSR1)

Put this anywhere in your code that you want to send the signal from.

Answer from moomima on Stack Overflow
🌐
Python
docs.python.org › 3 › library › signal.html
signal — Set handlers for asynchronous events
Send the signal signalnum to the thread thread_id, another thread in the same process as the caller. The target thread can be executing any code (Python or not). However, if the target thread is executing the Python interpreter, the Python signal ...
Discussions

Os.kill Signals not being received correctly, alternative is kill -SIGUSR1 command
I’m trying to use the os.kill, Popen.send_signal, or otherwise some kind of pure-python implementation of sending a signal to a process. Unfortunately, none of them work, and instead, I spawn a kill process to do it for me with Popen. You can read my original issue on StackOverflow here, ... More on discuss.python.org
🌐 discuss.python.org
7
0
May 19, 2023
python - How to send signal from subprocess to parent process? - Stack Overflow
Note: the child crashes the second time it runs sisr, because then the parent has exited, so os.getppid() return 0 and sending a signal to process 0 is forbidden. More on stackoverflow.com
🌐 stackoverflow.com
linux - Send SIGINT in python to os.system - Stack Overflow
I am trying to run a Linux command strace -c ./client in python with os.system(). When I press Ctrl + C I get some output on the terminal. I have to send the "Process correctly halted" signal programmatically after one minute and want the terminal output that is produced after pressing Ctrl ... More on stackoverflow.com
🌐 stackoverflow.com
python - Send signals to processes by name - Stack Overflow
I would like to do the equivalent of killall -s USR1 termite in a python script. Of course I can do os.system('killall -s USR1 termite') which works fine, but this doesn't look very elegant to me.... More on stackoverflow.com
🌐 stackoverflow.com
April 11, 2017
🌐
Reddit
reddit.com › r/learnpython › sending a custom 'signal' to a running script?
r/learnpython on Reddit: Sending a custom 'signal' to a running script?
November 3, 2020 -

Hi. New to python so not sure if this is possible. I know that python can handle trapping regular OS signals like SIGTERM etc and handling that in a custom fashion, however, is there a way to send a custom signal to python?

To attempt to clarify what I mean, I have a script that is running and processing actions based on a provided config file. I want to be able to send some sort of signal to the script to tell it to deviate from the current action and execute another method. I am currently achieving this by having it check if a file exists at various points of the code and that gets the job done, but is there a more elegant way to do what I am describing? Thanks in advance.

Top answer
1 of 4
1
In Linux, it's easy: kill [pid] or, if the process name is unique, killall [name] or pkill [name]. You can pass arguments like -TERM, -KILL, -USR1 or their numerical equivalents, e.g. kill -9 is common to kill a process. In Windows, I'm not sure how to do it, but the fact that this project exists makes me think it's not super straightforward with the tools that are already installed: https://github.com/alirdn/windows-kill If you meant to send the signal from Python, then, yes, it's easy: >>> from os import kill >>> from signal import SIGTERM >>> kill(38790, SIGTERM) You can look up the PIDs manually or with psutil or something.
2 of 4
1
Definitely possible, but you need to think about how it will work. Right now it sounds like you have something that just loops along and checks to see if the file changed at one point in the loop. In contrast, if you send the process a signal, that could show up anytime in the loop, meaning that you will need to allow your program to handle it asynchronously. You could make your flagging a bit more elegant by using shared memory between two python programs. One to do the processing you are currently doing and a second to flip the switch in the shared memory. using mmap That's doing the same thing as your file, but maybe a little neater (and you could have the results of the analysis dumped to memory so you could see them in the other program. This is a bit of a pain though, as it involves a fair bit of structing out the data you want to share, iirc. You could also accomplish the same thing with threading. One thread processes and the other thread takes commands and puts them into shared memory. If you are willing to go full async, check out some of the RPC libraries. This one looks nice: https://rpyc.readthedocs.io/en/latest/ That's definitely the elegant way to do it. Again, you'd have two programs, one asynchronous that processes the data and the second that sends it signals and gets back information. This also has the advantage that you do not need to find the original process and ping it. Another way would be to implement some sort of feeder queue that handles passing the information to be processed and instructions together.
🌐
Coderz Column
coderzcolumn.com › tutorials › python › signal-simple-guide-to-send-receive-and-handle-system-signals-in-python
signal - Simple Guide to Send, Receive and Handle System Signals in Python by Sunny Solanki
A comprehensive guide on how to use Python module "signal" to send, receive and handle system (Unix/Windows) signals to signal some event. The library lets us catch signal and run a handler (callback) based on event represented by signal. The signal can be sent to different threads and processes to inform them about event and execute handler by them accordingly.
🌐
Stack Abuse
stackabuse.com › handling-unix-signals-in-python
Handling Unix Signals in Python
November 30, 2018 - The following example command sends the signal 15 (SIGTERM) to the process that has the pid 12345: ... Which way you choose depends on what is more convenient for you. Both ways have the same effect. As a result the process receives the signal SIGTERM, and terminates immediately. Since Python 1.4, ...
🌐
Julienharbulot
julienharbulot.com › python-signals.html
How to hangle signals in python? - Julien Harbulot
December 27, 2018 - Signal are a means by which you can communicate with a process (or processes can communicate between each others). The two most popular signals are SIGINT and SIGTSTP. You can send a SIGINT signal to the process currently running in your terminal with ctrl+c or cmd+c.
🌐
Python Module of the Week
pymotw.com › 3 › signal
signal — Asynchronous System Events
April 22, 2017 - Send signals to the running program using os.kill() or the Unix command line program kill. $ python3 signal_signal.py My PID is: 71387 Waiting... Waiting... Waiting... Received: 30 Waiting... Waiting... Received: 31 Waiting...
🌐
Python
docs.python.org › 3.4 › library › signal.html
18.8. signal — Set handlers for asynchronous events — Python 3.4.10 documentation
June 16, 2019 - When an interval timer fires, a signal is sent to the process. The signal sent is dependent on the timer being used; signal.ITIMER_REAL will deliver SIGALRM, signal.ITIMER_VIRTUAL sends SIGVTALRM, and signal.ITIMER_PROF will deliver SIGPROF.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-os-kill-method
Python | os.kill() method - GeeksforGeeks
July 12, 2025 - os.kill() method in Python is used to send a specified signal to the process with a specified process ID.
🌐
Python.org
discuss.python.org › python help
Os.kill Signals not being received correctly, alternative is kill -SIGUSR1 command - Python Help - Discussions on Python.org
May 19, 2023 - I’m trying to use the os.kill, Popen.send_signal, or otherwise some kind of pure-python implementation of sending a signal to a process. Unfortunately, none of them work, and instead, I spawn a kill process to do it for me with Popen. You can read my original issue on StackOverflow here, ...
🌐
Linux Hint
linuxhint.com › send-catch-sigterm-bash-and-python
How to Send and Catch SIGTERM in Bash and Python – Linux Hint
When using the Python 1.4 and latest versions, you can utilize the signal library to send and catch SIGTERM. Import the library in your program to define how your program should capture and react to different signals. The signal library lets you create a signal handler to report the integer of the received signal. You can then register the captured signal and get the information about the current process such as its PID.
Top answer
1 of 1
6

Your sisr handler never executes.

signal.setitimer(signal.ITIMER_VIRTUAL, 1, 1)

This line sets a virtual timer, which, according to documentation, “Decrements interval timer only when the process is executing, and delivers SIGVTALRM upon expiration”.

The thing is, your process is almost never executing. The prints take almost no time inside your process, all the work is done by the kernel delivering the output to your console application (xterm, konsole, …) and the application repainting the screen. Meanwhile, your child process is asleep, and the timer does not run.

Change it with a real timer, it works :)

import signal
import os
from time import sleep


def isr(signum, frame):
    print "Hey, I'm the ISR!"

signal.signal(signal.SIGALRM, isr)

pid = os.fork()
if pid == 0:
    def sisr(signum, frame):        
        print "Child running sisr"
        os.kill(os.getppid(), signal.SIGALRM)

    signal.signal(signal.SIGALRM, sisr)
    signal.setitimer(signal.ITIMER_REAL, 1, 1)

    while True:
        print "2"
        sleep(1)
else:
    sleep(10)
    print "Parent quitting"

Output:

spectras@etherbee:~/temp$ python test.py
2
Child running sisr    
2
Hey, I'm the ISR!
Parent quitting
spectras@etherbee:~/temp$ Child running sisr

Traceback (most recent call last):
  File "test.py", line 22, in <module>
    sleep(1)
  File "test.py", line 15, in sisr
    os.kill(os.getppid(), signal.SIGALRM)
OSError: [Errno 1] Operation not permitted

Note: the child crashes the second time it runs sisr, because then the parent has exited, so os.getppid() return 0 and sending a signal to process 0 is forbidden.

🌐
Python Forum
python-forum.io › thread-11203.html
Subprocess.send_signal, wait until completion
Hello, I have a subprocess running named proc and need to send it a USR1 signal. To do that, I'm using proc.send_signal(signal.SIGUSR1). The problem is that it takes some time for the process to do wh
🌐
YouTube
youtube.com › watch
Processing & Handling Signals in Python - YouTube
Today we learn how to process and handle signals in Python.◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾📚 Programming Books & Merch 📚🐍 The Python Bible Book: https://www.neuralnine.c...
Published   December 1, 2022
🌐
Python Module of the Week
pymotw.com › 2 › signal
signal – Receive notification of asynchronous system events - Python Module of the Week
To send signals to the running program, I use the command line program kill. To produce the output below, I ran signal_signal.py in one window, then kill -USR1 $pid, kill -USR2 $pid, and kill -INT $pid in another. $ python signal_signal.py My PID is: 71387 Waiting...
Top answer
1 of 1
1

You're invoking ssh with the -T option, meaning that it won't allocate a PTY (pseudo-TTY) for the remote session. In this case, there's no way to signal the remote process through that ssh session.

The SSH protocol has a message to send a signal to the remote process. However, you're probably using OpenSSH for either the client or the server or both, and as far as I can tell, OpenSSH doesn't implement the signal message. So the OpenSSH client can't send the message, and the OpenSSH server won't act on it.

There is an SSH extension to send a "break" message which is supported by OpenSSH. In an interactive session, the OpenSSH client has an escape sequence that you can type to send a break to the server. The OpenSSH server handles break messages by sending a break to the PTY for the remote session, and unix PTYs will normally treat a break as a SIGINT. However, breaks are fundamentally a TTY concept, and none of this will work for remote sessions which don't have a PTY.

I can think of two ways to do what you want:

  1. Invoke ssh with the -tt parameter instead of -T. This will cause ssh to request a TTY for the remote session. Running the remote process through a TTY will make it act like it's running interactively. Killing the local ssh process should cause the remote process to receive a SIGHUP. Writing a Ctrl-C to the local ssh process's standard input should cause the remote process to receive a SIGINT.

  2. Open another ssh session to the remote host and use killall or some other command to signal the process that you want to signal.

🌐
Medium
medium.com › fintechexplained › advanced-python-how-to-use-signal-driven-programming-in-applications-84fcb722a369
Advanced Python: How To Use Signal Driven Programming In Applications | by Farhad Malik | FinTechExplained | Medium
July 26, 2020 - Advanced Python: How To Use Signal Driven Programming In Applications Learn What Signaling Is And How To Generate Signals In Your Python Application Operating systems use signals to communicate with …
🌐
Masterccc
masterccc.github.io › memo › process_com
Python subprocess communication · Master 0xCcC
October 4, 2019 - Cheatsheet - Python subprocess communication Link to heading Imports Link to heading from subprocess import Popen, PIPE Imports for signals Link to heading import signal, os Start process Link to heading process = Popen(['./sum 50'], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) Check if process is alive Link to heading def isAlive(process): poll = process.poll() if poll == None: print("Program running...") else: print("Program stopped :(") Read from stdout Link to heading def getline(process): return "stdout : " + process.