Ruiqi Li
Ruiqi Li

Reputation: 197

Checking if process is completed (python)

How do I check if a process has stopped running in python? I don't care if it is a zombie process, just if it has stopped running (syscall exit) This only has to work on linux. I have checked out psutil, but the STATE_STOPPED and STATE_DEAD don't seem to work

Upvotes: 1

Views: 571

Answers (1)

Enzo Caceres
Enzo Caceres

Reputation: 519

pidof -- find the process ID of a running program.

pidof will have his exit code to zero if at least one process by that name is running, else it will be one.

You can do a little bash script like this:

if pidof python > /dev/null;
then
    echo 'At least one is running'
else
    echo 'No process is running'
fi

I am pipe-ing the output to /dev/null just to avoid having the PID of the found process being printed, but this is optional.

EDIT: If you want to be able to do it from python, just do the same thing:

import os

if os.system("pidof man > /dev/null") == 0:
    print('At least one is running')
else:
    print('No process is running')

Upvotes: 2

Related Questions