Linux Command Reference ยท Processes and Signals
Linux timeout command
Reference for the Linux timeout command: stop a command after a time limit, pick the signal, send KILL as a fallback, read exit status 124 and use it in scripts.
timeout runs a command and ends it if it is still running after a given duration. Use it to protect scripts from hanging network calls, stuck programs and endless loops. The command's output is passed through unchanged; timeout only adds the deadline.
When the limit is reached it sends SIGTERM by default, then reports the situation with exit status 124. A program that ignores SIGTERM can be followed up with SIGKILL through -k.
timeout [OPTION] DURATION COMMAND [ARG]...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
timeout [-k KILL_AFTER] [-s SIGNAL] [--foreground] [--preserve-status] DURATION COMMAND [ARG]...
DURATION is a number with an optional suffix: s (seconds, the default), m, h or d. Fractions are allowed: 0.5, 1.5m. A duration of 0 disables the limit.
| Option | Description |
|---|---|
-s SIGNAL |
Signal to send at the deadline (default TERM). |
-k DURATION |
If the command is still alive this long after the first signal, send KILL. |
--preserve-status |
Exit with the command's own status even when it timed out. |
--foreground |
Run the command in the foreground; needed for interactive programs, but children are then not signalled. |
-v |
Print a diagnostic when a signal is sent. |
Returns:
- 124: the command timed out (without
--preserve-status). - 125:
timeoutitself failed. - 126: the command was found but could not be run.
- 127: the command was not found.
- 137: it was killed by
KILL(via-kor an outside kill). - otherwise: the command's own exit status.
Usage notes
- Check
124, not just failure.if timeout 10 curl ...; thencannot tell a slow server from a refused request; test$? -eq 124when it matters. - Always add
-kfor unreliable tools. Without it a program that traps or ignoresTERMruns forever. - Children.
timeoutruns the command in its own process group and signals the group, so helpers started by the command are included (unless--foreground). - Shell functions and builtins are not commands.
timeout 5 myfuncfails; usetimeout 5 bash -c 'myfunc'afterexport -f myfunc, or an external program. - Pipelines. Put
timeoutin front of the command that might hang, not the whole pipeline:timeout 5 slow | sort. - Compared with ulimit -t:
ulimit -tlimits CPU time used,timeoutlimits wall-clock time. - Part of GNU coreutils; BSD and macOS lack it unless installed as
gtimeout.
Related
sleep,time,kill,ulimit- Lessons: Kill and signals, Traps and signals