all Python 3osfunction
os.kill
os — signals and priority
Send a signal to a process by PID.
Syntax
os.kill(...)
On Unix signal 0 sends nothing and checks that the process is alive and reachable. Not on Windows: any signal other than CTRL_C_EVENT and CTRL_BREAK_EVENT unconditionally terminates the process with an exit code equal to the signal number — so kill(pid, 0) kills it with code 0. A PID may already belong to another process, so signalling an old number is risky.
Examples
import os
import signal
pid = os.fork() # → ? только Unix: 0 в дочернем процессе, PID ребёнка — в родительском
if pid: os.kill(pid, signal.SIGTERM) # → ? родитель вежливо просит ребёнка завершиться, возвращается None
if pid: os.kill(pid, 0) # → ? «нулевой» сигнал ничего не шлёт: так проверяют, жив ли процесс
if pid: os.kill(pid, signal.SIGKILL) # → ? только Unix: безусловное убийство, обработчик его перехватить не может
os.kill(999999, signal.SIGTERM) # → ProcessLookupError: процесса с таким PID нет
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy