Previous:stdbuf
: Run a command with modified I/O stream buffering, Up:Modified command invocation [Contents][Index]
timeout
: Run a command with a time limit ¶timeout
runs the givencommand and kills it if it isstill running after the specified time interval. Synopsis:
timeout [option]durationcommand [arg]...
command must not be a special built-in utility (seeSpecial built-in utilities).
The program accepts the following options. Also seeCommon options.Options must precede operands.
Don’t create a separate background program group, so thatthe managedcommand can use the foreground TTY normally.This is needed to support two situations when timing out commands,when not invokingtimeout
from an interactive shell.
In this mode of operation, any children ofcommandwill not be timed out. Also SIGCONT will not be sent tocommand,as it’s generally not needed with foreground processes, and cancause intermittent signal delivery issues with programs that are monitorsthemselves (like GDB for example).
Ensure the monitoredcommand is killed by also sending a ‘KILL’signal.
The specifiedduration starts from the point in time whentimeout
sends the initial signal tocommand, i.e.,not from the beginning when thecommand is started.
This option has no effect if either the maindurationof thetimeout
command, or theduration specifiedto this option, is 0.
This option may be useful if the selected signal did not kill thecommand,either because the signal was blocked or ignored, or if thecommand takestoo long (e.g. for cleanup work) to terminate itself within a certain amountof time.
Return the exit status of the managedcommand on timeout, rather thana specific exit status indicating a timeout. This is useful if themanagedcommand supports running for an indeterminate amount of time.
Send thissignal tocommand on timeout, rather than thedefault ‘TERM’ signal.signal may be a name like ‘HUP’or a number. SeeSignal specifications.
Diagnose to standard error, any signal sent upon timeout.
duration is a floating point number in either the current or theC locale (seeFloating point numbers) followed by an optional unit:
‘s’ for seconds (the default)‘m’ for minutes‘h’ for hours‘d’ for days
A hexadecimal number can precede a ‘d’ suffix only if the numberhas a ‘p’ style exponent, e.g., ‘0x1p0d’ means one day.A duration of 0 disables the associated timeout.The actual timeout duration is dependent on system conditions,which should be especially considered when specifying sub-second timeouts.
Exit status:
124 ifcommand times out, and--preserve-status is not specified125 iftimeout
itself fails126 ifcommand is found but cannot be invoked127 ifcommand cannot be found137 ifcommand ortimeout
is sent the KILL(9) signal (128+9)the exit status ofcommand otherwise
In the case of the ‘KILL(9)’ signal,timeout
returns withexit status 137, regardless of whether that signal is sent tocommandor totimeout
itself, i.e., these cases cannot be distinguished.In the latter case, thecommand process may still be alive aftertimeout
has forcefully been terminated.
Examples:
# Send the default TERM signal after 20s to a short-living 'sleep 1'.# As that terminates long before the given duration, 'timeout' returns# with the same exit status as the command, 0 in this case.timeout 20 sleep 1# Send the INT signal after 5s to the 'sleep' command. Returns after# 5 seconds with exit status 124 to indicate the sending of the signal.timeout -s INT 5 sleep 20# Likewise, but the command ignoring the INT signal due to being started# via 'env --ignore-signal'. Thus, 'sleep' terminates regularly after# the full 20 seconds, still 'timeout' returns with exit status 124.timeout -s INT 5s env --ignore-signal=INT sleep 20# Likewise, but sending the KILL signal 3 seconds after the initial# INT signal. Hence, 'sleep' is forcefully terminated after about# 8 seconds (5+3), and 'timeout' returns with an exit status of 137.timeout -s INT -k 3s 5s env --ignore-signal=INT sleep 20
Previous:stdbuf
: Run a command with modified I/O stream buffering, Up:Modified command invocation [Contents][Index]