2

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?

  • ps -ef | grep test will always show positive, since the command itself includes the string test :). to be able to choose the best option, you need to mention what kind of process it is . A script? a regular application? A local one? Run by root? – Jacob Vlijm Apr 21 '16 at 20:43
  • to be specific, it's a unit test script. – beginner_user Apr 21 '16 at 20:48
2

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

Replace <process_name> with the actual name of the process you want to track.

  • @heemaly, thanks a lot! used grep along with ps since pgrep doesn't work for me#!/bin/bash counter=0 while :; do [ "$counter" -eq 6 ] && break if ps -ef|grep unit_test01 &>/dev/null; then echo "Success !!" (( counter += 1 )) sleep 10 else echo "Process not running..exiting !!" break fi done – beginner_user Apr 21 '16 at 22:04
0

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, echo success.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.