How to Kill a Process or Capture Signals (SIGTERM, SIGKILL) in Python? | ninjasquad
Kill Current Process
We can kill a process itself or let the process commit suicide by sending the SIGKILL signal to itself. We can do this via os.kill:
1 2 3 4 |
import os import signal os.kill(os.getpid(), signal.SIGKILL) |
import os import signal os.kill(os.getpid(), signal.SIGKILL)
The SIGKILL will terminate the Python process immediately. Alternatively, we can graceful terminate a process by sending the SIGTERM signal, which is a gentle way to ask the process to terminate itself. In this way, the process can finish up some work e.g. clean up, before being killed.
1 2 3 4 |
import os import signal os.kill(os.getpid(), signal.SIGTERM) |
import os import signal os.kill(os.getpid(), signal.SIGTERM)
Kill the Parent Process
We can pass the parents PID to os.kill via os.getppid():
1 2 3 4 |
import os import signal os.kill(os.getppid(), signal.SIGKILL) |
import os import signal os.kill(os.getppid(), signal.SIGKILL)
The Children Processes are also terminated when their parents are killed.
Capture or Trap the Signals (SIGTERM) in Python
The SIGKILL can’t be captured and handled as the process is terminated straight away. But for other signals (we can check by “kill -l” command), we can capture and handle them:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX |
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
For example, we can use the signal.signal to write a handler to trap the signals. Sending a SIGTERM to following and the console will print “Received Signal 15”, and if the program receives SIGINT – which is basically pressing the Ctrl + C shortcut to interrupt, it will print “Received Signal 2”.
1 2 3 4 5 |
def capture_signal(signal_number, frame): print('Received Signal {}'.format(signal_number)) sys.exit(1) signal.signal(signal.SIGTERM, handler) |
def capture_signal(signal_number, frame): print('Received Signal {}'.format(signal_number)) sys.exit(1) signal.signal(signal.SIGTERM, handler)
The second parameter “frame” is a frame object:
1 |
<frame at 0x7f798c22a4c0, file 'signal_test.py', line 130, code run> |
<frame at 0x7f798c22a4c0, file 'signal_test.py', line 130, code run>
Conclusion
We can use the os.kill to either kill a process itself, the parent process or any other process (as long as the current user running the process has permissions). To retrieve the current process ID, we can do os.getpid(), the parent process’s ID can be retrieved by os.getppid().
The SIGKILL can’t be captured and handled. To view a list of all Signals, we can do “kill -l“.
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading…
614 words
Last Post: How Tall is the Table? (Simple Math Equations)
Source: Internet