Monday, February 26, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  10] [ 0]  / answers: 1 / hits: 1623  / 1 Year ago, sun, january 8, 2023, 6:23:54

How can i check in a script if a certain process (PID) is still running?


e.g.
check if PID still running
if yes then keep checking
if not then echo PID has finished and send simple email

or
Check if PID is not running
echo PID not running and send simple email
else
go back and check again

Additional Info:
Mail will be a simple command that gets executed when the PID is gone, like mail -s "PID update" [email protected] <<< "PID $pid has exited on $HOSTENAME on $(date)" –


Additional info: the PID will be active for several days and so the monitoring script has to continue checking in the background for a long time


More From » scripts

 Answers
7

Intoduction


From man kill(1):



If signal is 0, then no actual signal is sent, but error checking is
still performed.



Therefore, to check if a process(that the current user/caller is permitted to signal) is running, you can use either /bin/kill or if your shell has a builtin kill command, then you can use that as well ... kill will exit with exit code 0(i.e. success) if the process exists and can be killed(by the current user/caller) and it will error, print an error message and exit > 0 otherwise.


Notice: If, the current user/caller is not permitted to signal the process, then please see the workarounds section below on how to do it.


Usage


In bash, however, you can use the shell builtin kill to do things like this:


pid="34223"

kill -0 "$pid" &> /dev/null && echo "$pid exists" || echo "$pid doesn't exist"

and this:


pid="34223"

if kill -0 "$pid" &> /dev/null;
then
echo "$pid exists"
else
echo "$pid doesn't exist"
fi

and this:


pid="34223"

while :;
do
if ! kill -0 "$pid" &> /dev/null;
then
echo "$pid has exited"
break
else
sleep 1
fi
done

or to run the above example from within a script that its PID is to be monitored without stopping the execution of the script, you can do this:


#!/bin/bash

# Put this at the top to be run in a background sub-shel i.e (...) &

pid="$$"

(while :;
do
if ! kill -0 "$pid" &> /dev/null;
then
echo "$pid has exited"
break
else
sleep 1
fi
done) &

# The rest of your code goes below this line

Without actually killing anything.


Workarounds


Notice: Process IDs in Ubuntu get recycled and reused for newly created processes once their old processes die and the same application/program most likely will get assigned different PIDs the next time it is run ... So, they are not unique and not persistent ... Therefore, if you want to know when a process has just started, you can use e.g. pgrep to find process by e.g command/name as PID doesn't make sense here:


cmd="my_command"

while :;
do
if pgrep -f "$cmd" &> /dev/null;
then
echo "$cmd has started"
break
else
sleep 1
fi
done

Notice: as well that as stated in man kill(2):



If sig is 0, then no signal is sent, but existence and permission
checks are still performed; this can be used to check for the
existence of a process ID or process group ID that the caller is
permitted to signal.



Therefore, if the the user running the above kill checks needs to check processes that belong to other users/groups, then they need to implement, instead of the above, something like this:


pid="34223"

if ! kill -0 "$pid" |& grep -q "No such process";
then
echo "$pid exists"
else
echo "$pid doesn't exist"
fi

This will check the error message itself instead of the exit status for kill and therefore should work as the error message when the process ID exists but the signaling user isn't permitted to do so will read Operation not permitted while if that process doesn't exist, it will read No such process ... Therefore, checking for the latter should work.


[#15] Tuesday, January 10, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eballxeye

Total Points: 370
Total Questions: 91
Total Answers: 139

Location: Suriname
Member since Sat, Jan 1, 2022
2 Years ago
eballxeye questions
;