Re: Running a command for a limited time



On Sat, 05 Jul 2008 19:47:36 -0300, Lethal Possum <lethal.possum@xxxxxxxxx> wrote:

On 25 juin, 11:09, "Joachim Schmitz" <nospam.j...@xxxxxxxxxxxxxxxxxx>
wrote:
Alvin SIU wrote:
On 6月23日, 下午5時31分, Lethal Possum <lethal.pos...@xxxxxxxxx> wrote:
Hello everyone,

I am looking for a command line utility that would allow me to
specify the maximum amount of time a shell script can run. Basically
the idea is that I run a rsync in a nightly cron job and I would
like to make sure it does not run for more than 3 hours (else I'd
rather have it killed as it would properly resume its work on the
next day). Any suggestion?

By the way I am running Debian "Etch" so a tool available as a Debian
package would be nice.

Many thanks,

Thomas

Hi,

I also face similar situation many years ago.
It is also about 3 hour, sleeping, PID re-cycling, etc.
At that time, I also search the internet for help.

I come across a suggestion like this:
-------------------------------------------
Let the parent process be A
Let the child process be B
A kick-out B
A get B's PID
A pass its own PID to B (as env variable, for example)
A sleep 3 hour.
For B,
if it finishes before 3 hour,
B has to send a special signal to A

This does happen automatically, the signal is SIGCHLD. By default that is
ignored, so A needs to catch it

in C:
signal(SIGCHLD, exit);

in sh:
trap exit SIGCHLD

Changes my shell-script posted earlier:
$ cat watchdog
#!/bin/ksh
if [ $# -lt 2 ]
then
echo "Usage: $0 timeout command [options]" >&2
exit 1
fi
timeout=$1;shift
# switch on job control
set -m
# catch SIGCHLD
trap "exit" SIGCHLD
$@ &
echo "Monitoring \"$@\" (PID: $!) for $timeout seconds"
sleep $timeout
echo "Killing \"$1\" (PID: $!) as it ran longer then $timeout seconds
kill %1 || { echo give some time to settle; sleep 10; kill -KILL %1; }



(using A's PID) to notify A before B ends
For A,
if it receives that special signal,
A will stop sleeping and just end.
If it does not receive that special signal,
after sleeping 3 hour, wake up and kill B
(killing B can use another special signal
so that B can do some data wrap up
before committing suicide on receiving that
special signal)
----------------------------------------

The idea seems like that.
Sorry that it is long time ago and I really
cannot remember all the details.

Since at that time I am busy with other stuff,
I did not implement this idea in my scripts.

Hope that this story can help you solve the problem.

Bye
Alvin SIU

Bye, Jojo

Hi,

First of all thanks everyone for your input. Very interesting.

I adapted the latest script to bash and it works as advertised. The
only drawback is that the sleep command is not interrupted if the
child job finishes, so the script will always run for the complete
duration of the timeout. But that's actually not a big issue for me
(the child job will almost always run for more than the allowed time).

Thanks again.

Cheers,

Thomas


Another possibility for timer can be:
# sleep $timeout
read -t $timeout # for script in foreground (keyboard)
# or:
read -t $timeout </dev/tty9 # or other free tty, for background/foreground use

And for trap:
# trap "exit" SIGCHLD
trap 'kill -3 $$' SIGCHLD # to auto kill the script (^C equiv.)


I think that an advantange of the "read" is, as a shell's builtin, it will not
create an extra process.
.



Relevant Pages

  • Re: Running a command for a limited time
    ... A sleep 3 hour. ... This does happen automatically, the signal is SIGCHLD. ... trap exit SIGCHLD ... I adapted the latest script to bash and it works as advertised. ...
    (comp.unix.shell)
  • Re: A ksh problem about pgrep
    ... I met a strange ksh problem, can anyone give me some hints? ... # trap the signal and call Exit to exit the script ... trap 'Exit ' HUP INT QUIT TERM ...
    (comp.unix.shell)
  • [opensuse] Re: umm BASH gurus, what does the typo $!/bin/bash do?
    ... Doing a quick script, ... I had set a trap to remove a temp ... evidently the EXIT signal was generated and the trap ran -- even though it the ... Execute the script and check which process we are in now: ...
    (SuSE)
  • Re: at_exit handlers and Process.kill
    ... that will fire off no matter how the script is terminated? ... at_exit and trap the signal that you want but *please* do this ... *before* 'sleep 1 while true' ... exit}; sleep 1 while true' ...
    (comp.lang.ruby)
  • Re: A ksh problem about pgrep
    ... I met a strange ksh problem, can anyone give me some hints? ... # trap the signal and call Exit to exit the script ... trap 'Exit ' HUP INT QUIT TERM ...
    (comp.unix.shell)