Process monitoring for a fixed time

Hi Gurus,

I have a scenario where i need to monitor for a process which starts at a particular time. I need to check if the process has started at the specified time and keep checking this for 5 minutes only and then want to alert me.

ps -ef | grep -i process.sh | grep -v grep > path/process.txt
if [-s process.txt]
mail "Process started on time" abc@xyz.com
else
mail "Process did not start on time" abc@xyz.com
fi

The code above could be used for a particular time only. But we need the above to wait for every 5 seconds for 5 minutes continuously monitoring the process before executing it.

Thanks
Jay

for i in $(seq 1 60)
do
  sleep 5
  if [[ $(ps -eaf | grep [p]rocess.sh) ]]; then
    echo "process is running"
  else
    echo "process completed"
  fi
done

Hi Srini,

Thanks for the reply.

Sorry i am not able to understand the script as i am not familiar with seq.

I dont see the value of i being used anywhere in the script. I want the script to monitor the process for 5 minutes and for every 5 seconds in that 5 minutes.

I don't understand the requirements well enough to give you a complete answer. But here's a sample of how you can use "pkill" (if you system has it) to determine if a process is running or not. I think it's simpler and easier to understand than a pipeline of ps/grep.

RUNCHECK() { pkill -0 "$1" 2>/dev/null && echo "$1 is running" || echo "$1 NOT running"; }

Sending a "0" signal doesn't actually send any signal to the process. So you can't unintentionally interrupt it. It's just a simple way to find out if the process with the given pattern is running.

"seq" just produces a row of successive values, in this case "1 2 3 4 ..." up to 60. As the script is sleeping for 5 seconds running this 60 times will provide a running time of 5 minutes (60x5 seconds).

I hope this helps.

bakunin

seq is sequence.
I am waiting for 5 seconds, 60 times..which becomes every 5 seconds for 5 mins