$linuxjunkies
>

timeout(1)

Run a command with a time limit, killing it if it exceeds the specified duration.

UbuntuDebianFedoraArch

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

FlagWhat it does
-s, --signal=SIGNALSend SIGNAL to the process when timeout is reached (default: TERM). Use KILL for forceful termination.
-k, --kill-after=DURATIONSend KILL signal if process doesn't terminate after this additional duration following the initial signal.
--preserve-statusReturn the command's original exit status instead of timeout's exit code.
-v, --verbosePrint a message to stderr when the timeout is reached and the process is being killed.
--helpDisplay help message and exit.
--versionOutput version information and exit.

Examples

Run sleep for 20 seconds but kill it after 10 seconds; exits with code 124.

timeout 10 sleep 20

Execute a script with a 5-minute timeout limit.

timeout 5m ./long_script.sh

Download 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.zip

Run 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 /dest

Run 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

Related commands