os.kill
Signals, not killing: os.kill(pid, signal.SIGTERM) asks a process to stop; the process decides what to do unless the signal is SIGKILL. os.kill works on Unix and Windows (not WASI or iOS) but means different things there, killpg is Unix only, and abort ends the calling process without cleanup.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| pid | int | yes | kill: the target process id. On Unix 0 and negative values address process groups (see kill(2)). |
| pgid | int | yes | killpg: the process group id (Unix only). |
| sig | int | signal.Signals | yes | A signal number, best taken from the signal module (signal.SIGTERM, signal.SIGKILL on Unix). On Unix 0 sends nothing and only checks that the process exists. |
Return value
None — kill and killpg return None once the signal is sent (they do not wait for the process to end). abort never returns.
Common patterns
import os, signal, time os.kill(pid, signal.SIGTERM) time.sleep(5) try: os.kill(pid, signal.SIGKILL) except ProcessLookupError: pass # already gone
import os def pid_exists(pid): try: os.kill(pid, 0) except ProcessLookupError: return False except PermissionError: return True # exists, owned by someone else return True
import os, signal, subprocess p = subprocess.Popen(['./server.sh'], start_new_session=True) os.killpg(os.getpgid(p.pid), signal.SIGTERM) p.wait()
import subprocess p = subprocess.Popen(['long_job']) p.terminate() try: p.wait(timeout=5) except subprocess.TimeoutExpired: p.kill() p.wait()
Examples
Pitfalls
import os, signal, subprocess, sys p = subprocess.Popen([sys.executable, '-c', 'import sys; sys.stdin.read()'], stdin=subprocess.PIPE) os.kill(p.pid, signal.SIGTERM) rc = p.returncode p.wait() p.stdin.close() rc is None
import os, signal, subprocess, sys p = subprocess.Popen([sys.executable, '-c', 'import sys; sys.stdin.read()'], stdin=subprocess.PIPE) os.kill(p.pid, signal.SIGTERM) rc = p.wait() p.stdin.close() rc != 0
import os, signal, subprocess, sys p = subprocess.Popen([sys.executable, '-c', 'pass']) p.wait() try: os.kill(p, signal.SIGTERM) except TypeError as e: result = str(e) result
import subprocess, sys p = subprocess.Popen([sys.executable, '-c', 'pass']) p.wait() p.terminate() p.returncode
When to use
- Signalling a process you only know by pid (from a pid file, ps, psutil)
- killpg: stopping a child together with everything it started (Unix)
- abort: crash on purpose to get a core dump for debugging (Unix)
- Processes you started with subprocess → Popen.terminate() / kill() / send_signal()
- Checking whether a pid exists on Windows - os.kill(pid, 0) is not a test there
- Ending your own program normally → sys.exit()
Notes
FAQ
Killing processes cannot run inside the page, and the semantics differ: os.kill exists on Unix and Windows (not WASI or iOS), killpg is Unix only. On Windows only signal.CTRL_C_EVENT and CTRL_BREAK_EVENT are delivered as signals (to console processes sharing a console); any other value terminates the process with that number as exit code. The examples only signal a Python child they started and print results that are the same on both systems.