manpagez: man pages & more
info coreutils
Home | html | info | man

File: coreutils.info,  Node: timeout invocation,  Prev: stdbuf invocation,  Up: Modified command invocation

23.6 ‘timeout’: Run a command with a time limit
===============================================

‘timeout’ runs the given COMMAND and kills it if it is still running
after the specified time interval.  Synopsis:

     timeout [OPTION] DURATION COMMAND [ARG]...

   COMMAND must not be a special built-in utility (*note Special
built-in utilities::).

   The program accepts the following options.  Also see *note Common
options::.  Options must precede operands.

‘--preserve-status’
     Return the exit status of the managed COMMAND on timeout, rather
     than a specific exit status indicating a timeout.  This is useful
     if the managed COMMAND supports running for an indeterminate amount
     of time.

‘--foreground’
     Don’t create a separate background program group, so that the
     managed COMMAND can use the foreground TTY normally.  This is
     needed to support two situations when timing out commands, when not
     invoking ‘timeout’ from an interactive shell.
       1. COMMAND is interactive and needs to read from the terminal for
          example
       2. the user wants to support sending signals directly to COMMAND
          from the terminal (like Ctrl-C for example)

     Note in this mode of operation, any children of COMMAND will not be
     timed out.  Also SIGCONT will not be sent to COMMAND, as it’s
     generally not needed with foreground processes, and can cause
     intermittent signal delivery issues with programs that are monitors
     themselves (like GDB for example).

‘-k DURATION’
‘--kill-after=DURATION’
     Ensure the monitored COMMAND is killed by also sending a ‘KILL’
     signal.

     The specified DURATION starts from the point in time when ‘timeout’
     sends the initial signal to COMMAND, i.e., not from the beginning
     when the COMMAND is started.

     This option has no effect if either the main DURATION of the
     ‘timeout’ command, or the DURATION specified to this option, is 0.

     This option may be useful if the selected signal did not kill the
     COMMAND, either because the signal was blocked or ignored, or if
     the COMMAND takes too long (e.g.  for cleanup work) to terminate
     itself within a certain amount of time.

‘-s SIGNAL’
‘--signal=SIGNAL’
     Send this SIGNAL to COMMAND on timeout, rather than the default
     ‘TERM’ signal.  SIGNAL may be a name like ‘HUP’ or a number.  *Note
     Signal specifications::.

‘-v’
‘--verbose’
     Diagnose to standard error, any signal sent upon timeout.

   DURATION is a floating point number in either the current or the C
locale (*note Floating point::) followed by an optional unit:
     ‘s’ for seconds (the default)
     ‘m’ for minutes
     ‘h’ for hours
     ‘d’ for days
   A duration of 0 disables the associated timeout.  Note that the
actual timeout duration is dependent on system conditions, which should
be especially considered when specifying sub-second timeouts.

   Exit status:

     124 if COMMAND times out, and ‘--preserve-status’ is not specified
     125 if ‘timeout’ itself fails
     126 if COMMAND is found but cannot be invoked
     127 if COMMAND cannot be found
     137 if COMMAND or ‘timeout’ is sent the KILL(9) signal (128+9)
     the exit status of COMMAND otherwise

   In the case of the ‘KILL(9)’ signal, ‘timeout’ returns with exit
status 137, regardless of whether that signal is sent to COMMAND or to
‘timeout’ itself, i.e., these cases cannot be distinguished.  In the
latter case, the COMMAND process may still be alive after ‘timeout’ 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

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.