Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script Help - killing a job if its more then 2 hours old

Status
Not open for further replies.

Larshg

Programmer
Mar 1, 2001
187
DK
Hi

I have a job that starts in the crontab every minute – it only runs for 2 sec. – but in some rare cases it gets stuck. – this means that the process never dies, and this in turn means that it can’t start next time – and that means that the job stops working.

The only think to do is to kill the process, so that it can start up again.

I need to make a script that does this – the only thing the script should do is to kill the process IF it has been running for more then 2 hours


Hope some one can help
 
You can do it difficult or the easy way.
“ps” is the first way.

The script you start with the crontab must make a lock-file.

Now write the process id of the script in yourprogram.lck
echo $$ > /tmp/yourprogram.lck

The next time you run the program first check if yourprogram.lck exists.
If so do:
echo “-“ >> yourprogram.lck ## add a line
if [ $(cat /tmp/yourprogram.lck |wc –l) –lt 120 ] ;then
kill `head -1 yourprogram.lck`
else
exit
Fi
(Your program runs every minute.)

to sum up.
First check if the program is running.
When not: echo $$ > /tmp/yourprogram.lck
When yes:
If [ $(cat /tmp/yourprogram.lck |wc –l) –gt 120 ] ;then
Kill -9 `head -1 yourprogram.lck`
else
exit
fi
and
echo $$ > /tmp/yourprogram.lck
remove /tmp/yourprogram.lck when finished

When it is a binary program you run, run it from the script like:
Binary-program&
echo $! > /tmp/yourprogram.lck to capture the process id of the binary.
Now you can not remove yourprogram.lck.
First check if the program is running by:
if [ -n “$(ps –ef|grep $(head -1 yourprogram.lck)|grep –v grep) ]
then
echo …



Success


Gregor.Weertman@mailcity.com
 
Hmmm I not sure I fully understand your script - but I get the genneral ideer - to make a log file and check it thats if the process is still there in 2 houers.

But wouldn't it be posible to make the script check the systime in 'ps' command - all it needs to do is check if the time in the 'ps' is more then 2 hours old! (add to hours to the time and check with 'date' and kill it if it's not equal og less the the time it was startet)


 
You are right, but it is a drag.
You have to calculate with the time.

This is more work then counting the number of lines in the logfile.

When you would give me more detail,
I could give you more detailed help.

Regards Gregor.Weertman@mailcity.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top