timeout Command Cheat Sheet
timeout runs a command with a time limit. If the command runs longer than the specified duration, timeout kills it.
Synopsis
timeout [OPTION] DURATION COMMAND [ARG]...
Basic Usage
Limit Execution Time
Run ping for 5 seconds, then kill it.
timeout 5 ping google.com
With Units
s: seconds (default)m: minutesh: hoursd: days
timeout 1m script.sh
Signals
Custom Signal (-s)
By default, timeout sends SIGTERM. You can send SIGKILL (9) or SIGINT (2).
timeout -s SIGKILL 10s hung_process
Kill After Timeout (-k)
If the command catches SIGTERM and refuses to die, send SIGKILL after a grace period.
# Send SIGTERM after 10s. If still running after 15s (10+5), send SIGKILL.
timeout -k 5s 10s long_process.sh
Exit Status
- 124: Command timed out.
- 125: internal error.
- 126: command found but cannot be invoked.
- 127: command not found.
- Other: The exit status of the command itself (if it finished in time).
Preserve Status (--preserve-status)
By default, timeout returns 124 on timeout. Use this to return the command's own exit status (even if killed).
timeout --preserve-status 5s command
Notes
- Foreground:
timeoutnormally runs in the background. Use--foregroundif the command needs to interact with the TTY.