Thursday, July 9, 2009

Pause/Stop/Continue a process in Python

It took me a while to find the answer to this, so I figured I'd post this for anyone else looking for an answer. The method below allows you to pause a process so you can continue it later - the equivalent of Ctrl-Z and 'fg' in the terminal.

Use:
import os, signal
# Pause the process
os.kill(pid, signal.SIGSTOP)
# Continue the process
os.kill(pid, signal.SIGCONT)

# Pause a process and it's children
os.killpg(pid, signal.SIGSTOP)
# Continue a process and it's children
os.killpg(pid, signal.SIGCONT)


Hope this helps someone!