I have a process id in Python. I know I can kill it with os.kill(), but how do I check if it is alive? Is there a built-in function or do I have to go to the shell?
Use subprocess
module to spawn process. There is proc.poll() function - it returns None
if process is still alive, otherwise it returns process returncode.
os.kill
does not kill processes, it sends them signals (it's poorly named).
If you send signal 0, you can determine whether you are allowed to send other signals. An error code will indicate whether it's a permission problem or a missing process.
See man 2 kill
for more info.
Also, if the process is your child, you can get a SIGCHLD
when it dies, and you can use one of the wait
calls to deal with it.
With psutil you can check if a process id exists:
import psutil
print(psutil.pid_exists(1234))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.