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 OverflowYou can use os.kill():
os.kill(os.getpid(), signal.SIGUSR1)
Put this anywhere in your code that you want to send the signal from.
If you are willing to catch SIGALRM instead of SIGUSR1, try:
signal.alarm(10)
Otherwise, you'll need to start another thread:
import time, os, signal, threading
pid = os.getpid()
thread = threading.Thread(
target=lambda: (
time.sleep(10),
os.kill(pid, signal.SIGUSR1)))
thread.start()
Thus, this program:
import signal
import os
import time
def receive_signal(signum, stack):
print 'Received:', signum
signal.signal(signal.SIGUSR1, receive_signal)
signal.signal(signal.SIGUSR2, receive_signal)
signal.signal(signal.SIGALRM, receive_signal) # <-- THIS LINE ADDED
print 'My PID is:', os.getpid()
signal.alarm(10) # <-- THIS LINE ADDED
while True:
print 'Waiting...'
time.sleep(3)
produces this output:
$ python /tmp/x.py
My PID is: 3029
Waiting...
Waiting...
Waiting...
Waiting...
Received: 14
Waiting...
Waiting...
Os.kill Signals not being received correctly, alternative is kill -SIGUSR1 command
python - How to send signal from subprocess to parent process? - Stack Overflow
linux - Send SIGINT in python to os.system - Stack Overflow
python - Send signals to processes by name - Stack Overflow
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.
In Python, you could programatically send a Ctrl + C signal using os.kill. Problem is, you need the pid of the process that'll receive the signal, and os.system does not tell you anything about that. You should use subprocess for that. I don't quite get what you said about not getting the output on the terminal.
Anyways, here's how you could do it:
import subprocess
import signal
import os
devnull = open('/dev/null', 'w')
p = subprocess.Popen(["./main"], stdout=devnull, shell=False)
# Get the process id
pid = p.pid
os.kill(pid, signal.SIGINT)
if not p.poll():
print("Process correctly halted")
I would recommend subprocess python module for running linux commands. In that, SIGINT signal (equivalent to Ctrl + C keyboard interrupt) can be sent programmatically to a command using Popen.send_signal(signal.SIGINT) function. Popen.communicate() function will give you output. For example
import subprocess
import signal
..
process = subprocess.Popen(..) # pass cmd and args to the function
..
process.send_signal(signal.SIGINT) # send Ctrl-C signal
..
stdout, stderr = process.communicate() # get command output and error
..
Well you always have the subprocess module instead of OS , taking arguments, but if you don't find the the os.system or the call() elegant , I believe you have to implement you own class to create your abstraction layer.
Example with call
from subprocess import call
call("killall", "-s", "USR1", "terminate")
You could use the psutil package to find all processes matching your criteria (such as name). It has a method send_signal() (see docs).