Differences between revisions 7 and 22 (spanning 15 versions)
Revision 7 as of 2008-11-22 14:09:50
Size: 1652
Editor: localhost
Comment: converted to 1.6 markup
Revision 22 as of 2016-10-17 20:34:21
Size: 3387
Editor: edwardsnowden2
Comment: quotes
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
'''FIRST''' check whether the command you're running can be told to timeout directly. The methods described here are "hacky" workarounds to force a command to terminate after a certain time has elapsed. Configuring your command properly is ''always'' preferable to the alternatives below.
Line 4: Line 5:
There are two C programs that can do this: [[http://pilcrow.madison.wi.us/|doalarm]], and [[http://www.porcupine.org/forensics/tct.html|timeout]]. (Compiling them is beyond the scope of this document; suffice to say, it'll be trivial on GNU/Linux systems, easy on most BSDs, and painful on anything else....) If the command has no native support for stopping after a specified time, then the best alternatives are some external commands called [[http://www.porcupine.org/forensics/tct.html|timeout]] and [[http://pilcrow.madison.wi.us/|doalarm]]. Some Linux distributions offer the tct version of `timeout` as a package. There is also a GNU version of timeout, included in recent coreutils releases.
Line 6: Line 7:
If you don't have or don't want one of the above two programs, you can use a perl one-liner to set an ALRM and then exec the program you want to run under a time limit. In any case, you must understand what your program does with SIGALRM. Beware: by default, some implementations of `timeout` issue a SIGKILL (`kill -9`), which is roughly the same as pulling out the power cord (leaving no chance for the program to commit its work, often resulting in corruption of its data). You should use a signal that allows the program to shut itself down instead (SIGTERM). See ProcessManagement for more information on SIGKILL.

The primary difference between `doalarm` and `timeout` is that `doalarm` "execs" the program after setting up the alarm, which makes it wonderful in a WrapperScript; while `timeout` launches the program as a child and then hangs around (both processes exist simultaneously), which gives it the opportunity to send more than one signal if necessary.

If you don't have or don't want one of the above programs, you can use a perl one-liner to set an ALRM and then exec the program you want to run under a time limit. In any case, you must understand what your program does with SIGALRM; programs with periodic updates usually use ALRM for that purpose and update rather than dying when they receive that signal.
Line 9: Line 14:
doalarm() { perl -e 'alarm shift; exec @ARGV' "$@"; } doalarm() { perl -e 'alarm shift; exec @ARGV' -- "$@"; }
Line 13: Line 18:

If you can't or won't install one of these programs (which ''really'' should have been included with the basic core Unix utilities 30 years ago!), then the best you can do is an ugly hack like:
If you can't or won't use one of these programs (which ''really'' should have been included with the basic core Unix utilities 30 years ago!), then the best you can do is an ugly hack like:
Line 18: Line 22:
   { sleep 10; kill $pid; } &    { sleep 10; kill "$pid"; } &
Line 20: Line 24:
Line 23: Line 26:
The shell-script "[[http://www.shelldorado.com/scripts/cmds/timeout|timeout]]" uses the approach above. It has the advantage of working immediately (no need for compiling a program), but has problems e.g. with programs reading standard input. It is possible to do something similar, but to keep `command` in the foreground:
Line 25: Line 28:
Just use {{{doalarm}}} or {{{timeout}}} instead. Really. {{{
   sh -c '(sleep 10; kill "$$") & exec command'
}}}
`kill $$` would kill the shell, except that `exec` causes the command to take over the shell's PID. It is necessary to use `sh -c` so that the calling shell isn't replaced; in bash 4, it is possible to use a subshell instead:

{{{
   ( cmdpid=$BASHPID; (sleep 10; kill "$cmdpid") & exec command )
}}}
The shell-script "[[http://www.shelldorado.com/scripts/cmds/timeout|timeout]]" (not to be confused with the command 'timeout') uses the second approach above. It has the advantage of working immediately (no need for compiling a program), but has problems e.g. with programs reading standard input.

Just use {{{timeout}}} or {{{doalarm}}} instead. Really.

How do I run a command, and have it abort (timeout) after N seconds?

FIRST check whether the command you're running can be told to timeout directly. The methods described here are "hacky" workarounds to force a command to terminate after a certain time has elapsed. Configuring your command properly is always preferable to the alternatives below.

If the command has no native support for stopping after a specified time, then the best alternatives are some external commands called timeout and doalarm. Some Linux distributions offer the tct version of timeout as a package. There is also a GNU version of timeout, included in recent coreutils releases.

Beware: by default, some implementations of timeout issue a SIGKILL (kill -9), which is roughly the same as pulling out the power cord (leaving no chance for the program to commit its work, often resulting in corruption of its data). You should use a signal that allows the program to shut itself down instead (SIGTERM). See ProcessManagement for more information on SIGKILL.

The primary difference between doalarm and timeout is that doalarm "execs" the program after setting up the alarm, which makes it wonderful in a WrapperScript; while timeout launches the program as a child and then hangs around (both processes exist simultaneously), which gives it the opportunity to send more than one signal if necessary.

If you don't have or don't want one of the above programs, you can use a perl one-liner to set an ALRM and then exec the program you want to run under a time limit. In any case, you must understand what your program does with SIGALRM; programs with periodic updates usually use ALRM for that purpose and update rather than dying when they receive that signal.

doalarm() { perl -e 'alarm shift; exec @ARGV' -- "$@"; }

doalarm ${NUMBER_OF_SECONDS_BEFORE_ALRMING} program arg arg ...

If you can't or won't use one of these programs (which really should have been included with the basic core Unix utilities 30 years ago!), then the best you can do is an ugly hack like:

   command & pid=$!
   { sleep 10; kill "$pid"; } &

This will, as you will soon discover, produce quite a mess regardless of whether the timeout condition kicked in or not, if it's run in an interactive shell. Cleaning it up is not something worth my time. Also, it can't be used with any command that requires a foreground terminal, like top.

It is possible to do something similar, but to keep command in the foreground:

   sh -c '(sleep 10; kill "$$") & exec command'

kill $$ would kill the shell, except that exec causes the command to take over the shell's PID. It is necessary to use sh -c so that the calling shell isn't replaced; in bash 4, it is possible to use a subshell instead:

   ( cmdpid=$BASHPID; (sleep 10; kill "$cmdpid") & exec command )

The shell-script "timeout" (not to be confused with the command 'timeout') uses the second approach above. It has the advantage of working immediately (no need for compiling a program), but has problems e.g. with programs reading standard input.

Just use timeout or doalarm instead. Really.

BashFAQ/068 (last edited 2019-07-25 13:38:08 by GreyCat)