LinuxCommandLibrary

timeout

Run command with time limit

TLDR

Run a command with a time limit (30 seconds)

$ timeout 30s [command]
copy
Run with time limit in minutes
$ timeout 5m [command]
copy
Send specific signal when timeout occurs
$ timeout --signal=SIGKILL 10s [command]
copy
Kill after additional time if command doesn't terminate
$ timeout --kill-after=10s 30s [command]
copy
Preserve exit status of timed-out command
$ timeout --preserve-status 5s [command]
copy
Run with timeout in foreground (for interactive commands)
$ timeout --foreground 30s [command]
copy

SYNOPSIS

timeout [options] duration command [args...]

DESCRIPTION

timeout runs a command with a specified time limit. If the command doesn't complete within the duration, timeout sends a signal (SIGTERM by default) to terminate it.
The default signal (SIGTERM) allows processes to clean up before exiting. For processes that ignore SIGTERM, use --kill-after to send SIGKILL after an additional grace period.
Exit status is 124 if the command times out, 137 if killed by SIGKILL, or the command's own exit status if it completes within the limit. Use --preserve-status to return the signal number + 128 on timeout.

PARAMETERS

-k duration, --kill-after=duration

Send SIGKILL after additional duration if command still running
-s signal, --signal=signal
Signal to send on timeout (default: SIGTERM)
--preserve-status
Return command's exit status even on timeout
--foreground
Don't create new process group (for terminal interaction)
-v, --verbose
Diagnose signal sent to stderr

DURATION FORMAT

s: Seconds (default)
m: Minutes
h: Hours
d: Days
Numbers can be floating-point: 1.5m = 90 seconds

CAVEATS

Timeout creates a new process group by default, which can interfere with interactive commands or terminal I/O. Use --foreground for such cases.
Commands that spawn child processes may leave orphaned children. Timeout only signals the direct child, not grandchildren.
Very short timeouts (sub-second) may not be reliable due to scheduling overhead.

SEE ALSO

time(1), watch(1), kill(1), sleep(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community