Check if trigger Script is running

HI,

I have a script which will be running all the time...it is like a trigger.. wakesup every 10 minutes(trigger.sh) executes, and I want to write another script which monitors this script every one hour and if it finds that trigger script is not running it should start it and exit...and here is what i could think of doing..

try and grep the script name in the process to see if it is running like..
ps -ef | grep "trigger.sh"
but this is not working....I do not know why...
is theree any other way to try it out.

Thanks,

Have your trigger script write to a log

> logfile
echo "`date`" > logfile

Next search the forums for Perderabo's datecalc program.

Have your "check it" code:
Read the logfile, get the date it wrote.
Use datecalc to compare the time now with the time back when the entry was made.
If it was too long ago - restart the trigger

HI,
Please provide u r script which u used to monitor trigger.sh

This should do the trick...

if [[ `ps -ef | grep -v $$ | grep -c trigger.sh` -ne 1 ]]; then
#do whatever you feel appropriate
fi

After all sorts googling I narrowed it to this....

ps x | grep "trigger.sh" | awk '{ print $3 }' | grep -ic "A"

this gives me the number of active process either sleeping or running and it is Active...which should be greater than or equal to 2
BUT
this gives the results only for the current user.....does anybody know how to enhance it so that it can be used for all users....?

Thanks,

Alexop gave the solution to removing the current process - grep -v $$. Without this the process that runs grep "trigger.sh" always shows up. That is your process - the one looking to see if the script is running.

That means if the other process is not running, you have zero found. In your result, the trigger.sh script is not running. If are you positive it is supposed to be running ALL the time, then not finding it means it is not running. Your result of finding your own process and nothing else also means it is not running.

Hi,

The solution Alexop gave returns 6 if I am not running the script and returns 7 when I am running I noticed that it is showing all the processes that ran

this is what I get when that command ....

before running my trigger script ...
ps -ef | grep -v $$ | grep Trigger.sh
user1051  610404 1249490   0 09:29:11  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 1474632 1249490   0 09:30:59  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 2003114 1249490   0 09:27:59  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 2031834 1945810   0 10:02:56  pts/8  0:00 /usr/bin/sh ./Trigger.sh
user1051 2293766 1249490   0 09:46:03  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 2314464 1945810   0 11:34:41  pts/8  0:00 /usr/bin/sh ./Trigger.sh

while running Trigger script...
user1051 233596 1249490   0 09:50:12  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 610404 1249490   0 09:29:11  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 1474632 1249490   0 09:30:59  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 2003114 1249490   0 09:27:59  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 2031834 1945810   0 10:02:56  pts/8  0:00 /usr/bin/sh ./Trigger.sh
user1051 2293766 1249490   0 09:46:03  pts/3  0:00 /usr/bin/sh ./Trigger.sh
user1051 2314464 1945810   0 11:34:41  pts/8  0:00 /usr/bin/sh ./Trigger.sh

This could be because I ran the script for a while and stopped it and ran again, then it counts all the processes including the current one.....

It looks like you hit CTRL/Z when you were running your trigger.sh script, and now you have a bunch of bbackground processes.

Aren't you supposed to have one process that runs trigger.sh ?

Yes I did I was able to Kill all the process and It is working now...Thanks a lot.

My personal view:

Why do n't you setup a cron job to run the trigger.sh every 10 minutes or so ? You don't need extra monitoring.

Not sure, this satisfies your situation. Just my personal opinion.