Files
oam/knowledge base/timeout.md
2023-07-09 21:35:14 +02:00

1.7 KiB

timeout

Start a command and kill it if still running after a given duration.
The command must not be a special built-in utility.

Part of GNU coreutils.

Table of contents

  1. TL;DR
  2. Further readings
  3. Sources

TL;DR

# Send the default TERM signal after 20s to a short-living command.
# It terminates before the given duration limit, so 'timeout' returns with the
# same exit status as the command.
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
# interruption signal.
timeout -s INT 5 sleep 20

# Likewise, but the command will ignore the INT signal due to it being started
# via 'env --ignore-signal'.
# 'sleep' will terminate regularly after the full 20 seconds.
# 'timeout' will still return with exit status 124 to indicate the sending of
# the interruption signal.
timeout -s INT 5s env --ignore-signal=INT sleep 20

# Likewise, but will also send the KILL signal 3 seconds after the initial INT
# signal.
# 'sleep' is forcefully terminated after about 303 seconds (5m + 3s), and
# 'timeout' returns with an exit status of 137 to indicate the sending of the
# termination signal.
timeout -s INT -k 3s 5m env --ignore-signal=INT sleep 600

Further readings

Sources

All the references in the further readings section, plus the following: