timeout(1)
Run a command with a time limit, killing it if it exceeds the specified duration.
Synopsis
timeout [OPTION] DURATION COMMAND [ARG]...Description
timeout runs the specified command and terminates it if it does not finish within the given time duration. It is useful for preventing runaway processes, automating scripts that may hang, and enforcing strict time constraints on long-running operations.
The DURATION argument can be specified in seconds (e.g., 10), with a suffix (e.g., 10s, 5m, 2h), or as floating-point values (e.g., 0.5s). If the command completes before the timeout, timeout exits with the command's status code.
Common options
| Flag | What it does |
|---|---|
-s, --signal=SIGNAL | Send SIGNAL to the process when timeout is reached (default: TERM). Use KILL for forceful termination. |
-k, --kill-after=DURATION | Send KILL signal if process doesn't terminate after this additional duration following the initial signal. |
--preserve-status | Return the command's original exit status instead of timeout's exit code. |
-v, --verbose | Print a message to stderr when the timeout is reached and the process is being killed. |
--help | Display help message and exit. |
--version | Output version information and exit. |
Examples
Run sleep for 20 seconds but kill it after 10 seconds; exits with code 124.
timeout 10 sleep 20Execute a script with a 5-minute timeout limit.
timeout 5m ./long_script.shDownload a file with a 30-second timeout; send KILL signal instead of TERM if it exceeds the limit.
timeout -s KILL 30 wget https://example.com/large-file.zipRun rsync with a 30-second timeout; if it hasn't terminated after TERM, wait 5 more seconds then send KILL.
timeout -k 5 30 rsync -avz /source /destRun mycommand with a 10-second timeout and check its actual exit status, not timeout's.
timeout --preserve-status 10 mycommand && echo 'Success'Ping with a 2-second timeout; print a message to stderr when the timeout occurs.
timeout -v 2 ping -c 10 example.com