My process name is
test. If I use
ps -ef | grep test
I see that process is running. Now I want to check this every 10 mins for one hour and print success if it's running successfully for one hour. What is the best way to do so?
You can use a
while loop; here i am using
pgrep to check if the process is running:
#!/bin/bash counter=0 while :; do [ "$counter" -eq 6 ] && break if pgrep <process_name> &>/dev/null; then echo "Success !!" (( counter += 1 )) sleep 10m else echo "Process not running..exiting !!" break fi done
<process_name> with the actual name of the process you want to track.
A simple loop in a script:
#! /bin/bash start=$SECONDS while (( SECONDS - start < 3600 )) do sleep 10m pgrep -f test &>- || exit done echo success
SECONDS is a special variable in bash that contains the number of seconds since the script has started.
pgrep test checks for a process named
test. If it doesn't find one, we exit the script. If not, we
sleep for 10 minutes.
If we didn't exit, then
pgrep was successful. So,