Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
timeout [OPTION] DURATION COMMAND [ARG]...

Try it

Bash Exit status 124 means the deadline hit

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.

Bash Stubborn programs: -k and -s

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

Terminal
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: timeout itself failed.
  • 126: the command was found but could not be run.
  • 127: the command was not found.
  • 137: it was killed by KILL (via -k or an outside kill).
  • otherwise: the command's own exit status.

Usage notes

  • Check 124, not just failure. if timeout 10 curl ...; then cannot tell a slow server from a refused request; test $? -eq 124 when it matters.
  • Always add -k for unreliable tools. Without it a program that traps or ignores TERM runs forever.
  • Children. timeout runs 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 myfunc fails; use timeout 5 bash -c 'myfunc' after export -f myfunc, or an external program.
  • Pipelines. Put timeout in front of the command that might hang, not the whole pipeline: timeout 5 slow | sort.
  • Compared with ulimit -t: ulimit -t limits CPU time used, timeout limits wall-clock time.
  • Part of GNU coreutils; BSD and macOS lack it unless installed as gtimeout.