What's a proper way of checking if a PID is running?
Solution 1
for most linux distros enumerating the /proc/{pid} is a good way to obtain information about the running processes, and usually how the userspace commands like "ps" are communicating with the kernel. So for example you can do;
[ -d "/proc/${kpid}" ] && echo "process exists" || echo "process not exists"
Edit: you should check that kpid is set, but this is more useful as it will return "not exists" for unset ${kpid}
[ -n "${kpid}" -a -d "/proc/${kpid}" ] && echo "process exists" || echo "process not exists"
Solution 2
As Anders noted, you should use kill -0
for POSIX compliance.
On Linux systems, you can also check for the existence of a file in the /proc filesystem, e.g.,
-f /proc/$(cat something.pid)/status
Solution 3
If this is in a script (which I assume is the case as you're worried about printing to stdout) then the following would be how you could do it:
if ps -p $(cat something.pid) > /dev/null 2>&1
then
kill $(cat something.pid)
else
# Deal with the fact that the process isn't running
# i.e. clear up the pid file
fi
The ps -p
looks for a process with the pid specified in something.pid (the $()
syntax is a slightly newer version of the backtick. Backtick needs escaping in certain circumstances which the new form doesn't). The 2>&1
redirects stderr for that command as well.
If the ps -p
command doesn't find the process with that PID it exits with a error > 0 and so the else
gets executed. Otherwise the kill
statement. You can negate the above if you want by doing:
if ! ps -p ...
Hope that answers your question. Obviously be careful and test a lot when using dangerous commands such as kill
.
Solution 4
If you have pid file you may use pgrep to check whether process is running:
if pgrep -F something.pid; then
echo "Running"
else
echo "Not running"
fi
On linux you may also check for existence of /proc/$pid
file:
if [ -d "/proc/${pid}" ]; then
echo "Running";
else
echo "Not running";
fi
Solution 5
Here are some options:
- If you're writing init-script (e.g. the one to place in
/etc/init.d/
) and if you're using Debian-based distro, you'd better usestart-stop-daemon
:
You should get exit code 0 if it's running.# start-stop-daemon -T -p $PIDFILE -u $USER_NAME -n $NAME
- You may use pgrep and pkill from
procps
package:pgrep --pidfile $PIDFILE
Related videos on Youtube
Jakub Arnold
Experienced software engineer with a background in machine learning and computer science and over 7 years of commercial practice of software development, looking to work on production quality software.
Updated on September 18, 2022Comments
-
Jakub Arnold over 1 year
I have a
.pid
file, and I need to check if the process is running. So far I found two optionskill -0 `cat something.pid`
which prints out an error if the pid isn't running. I know this can be redirected to
/dev/null
, but it makes me think that this isn't the best solution.The second solution would be to use
ps
, which however also prints on the STDOUTps -ef `cat something.pid`
Is it normal to redirect the output to
/dev/null
and just use the status code returned, or is it a sign that I'm doing something wrong and I need a different command?-
Anders about 12 yearsUse
kill -0
as it is standard (POSIX)-compliant. -
wisbucky almost 5 years
-
-
Wilf over 8 yearsNote with this you need to make sure
${kpid}
exists -
gokva over 8 years@Wilf I edited it, so it returns "process not exists" for unset ${kpid}
-
Wilf over 8 yearsThanks - I was doing a script that checks a PID using a similar method and it didn't work till I found this was the issue :)
-
Hayden Thring over 2 yearsCan you feed pgrep the pid as text rather than a pid file ?
-
Dima L. over 2 years@HaydenThring, on linux you can check for existence of /proc/$pid folder: if [ -d "/proc/${pid}" ]; then echo "Running"; else echo "Not running"; fi