Running processes in fixed time intervals

While messing around with Sun Cluster 3.2, I came across hatimerun. This nifty program can be used to run a program in a fixed amount of time, and kill the program if it runs longer that the time alloted to it. If hatimerun kills a program, it will return a status code of 99. If the program runs in it’s alloted time, hatimerun will return a status code of 0. To use hatimerun, you need to pass a time interval to the “-t” option, as well as a program to run in that time interval:

$ hatimerun -t 10 /bin/sleep 8

$ echo $?
0

$ hatimerun -t 10 /bin/sleep 12

$ echo $?
99

If anyone knows of a general purpose tool for doing this (preferably something that ships with Solaris or Redhat Enterprise Linux), I would appreciate it if you could leave a comment with further details.

2 Comments

Laen  on May 4th, 2007

That’s part of the hatools package.

http://www.fatalmind.com/software/hatools/

Anonymous Coward  on May 20th, 2007

I implemented something similar myself for my Linux systems. It’s way more kludgy but it’s very small and it has worked for me since years. I don’t understand why such a functionnality isn’t by default in Linux.

Basically I’ve got a “sleepkill” shellscript that looks like that:

sleep $TIME
kill -9 $PID

And I call it from other scripts like this:

processThatMayHang & (notice the ‘&’)
MAYNEEDTOBEKILLED=$!
sleepkill $MAYNEEDTOBEKILLED 2 > /dev/null 2>&1 &
SLEEP_PID=$!

wait $SLEEP_PID > /dev/null 2>&1

if [ $? -eq 0 ]
then
kill -9 $SLEEP_PID
fi

There are race conditions and it’s really fugly, but it works and, at worst, you’ll try to kill -9 a process that just terminated.

The procedure that calls “sleepkill” can be put in a second shell script and hence you’ve got a one liner.

Redirect output appropriately etc.

It’s just an example.

Leave a Comment