System and processes

The timeout command

Runs a command and stops it if it takes longer than the given time.

Syntax

timeout DURACIÓN COMANDOTry it in the terminal →

Most useful options

-s SEÑALSignal sent when time runs out. TERM by default; accepts a name such as KILL or a number.
-k DURACIÓNIf the command is still running after that extra wait, it sends KILL, which cannot be ignored.
-pReturns the same exit status as the command, even when time ran out.
-vWrites a notice to standard error each time it sends a signal.
-fLets the command read from the terminal and get keyboard signals. Its child processes then have no time limit.
s, m, h, dDuration suffixes: seconds by default, then minutes, hours and days. A 0 disables the limit.

Copy-ready examples

$ timeout 2 sleep 1finishes within the limit, so nothing is cut.
$ timeout 1 sleep 2; echo $?cuts it after 1 second and returns 124.

Example output

124
$ timeout -v 1 sleep 2the same, but reports the signal it sent.

Example output

timeout: sending signal TERM to command 'sleep'
$ timeout -k 5 60 ./procesosends TERM after 60 seconds and KILL five seconds later if it is still alive.
$ timeout 5m makegives the build five minutes at most.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
timeout: failed to run command 'nosuchcmd_xyz': No such file or directoryThe program does not exist or is not in PATH. In that case timeout ends with status 127.Write the full path, such as ./proceso, or check the name with which.
timeout: invalid time interval '5x'The duration is not a number with a valid suffix. timeout ends with status 125.Use a number with s, m, h or d, such as 5, 5s or 5m.

Typical mistakes

  • If it cuts the command, it returns status 124: that code tells you the time ran out.
  • With no suffix, the duration is in seconds: timeout 300 is the same as timeout 5m.
  • If the command ignores TERM it may stay alive: add -k so it gets KILL afterwards, which cannot be ignored.

Differences from similar commands

  • kill: sends a signal when you order it to. timeout sends it on its own when time runs out.
  • ulimit: limits the shell's resources, such as CPU time with -t. timeout limits wall-clock time, even when the process is waiting.
  • watch: repeats a command every few seconds, without end. timeout runs it once and cuts it if it does not finish in time.

Security

Stopping a command halfway can leave data incomplete. Do not put timeout in front of dd, a copy or a deletion you cannot repeat.

Related commands

FAQ

How do I know whether timeout cut the command or it finished earlier?

Check the exit status: 124 means it was cut. Any other value is the one the command itself returned.

Can I use timeout in a pipeline?

Yes, but the limit only affects the command it is placed in front of. In timeout 5 cat | wc -l, the time counts for cat, not for wc.

What happens if the program does not exit on TERM?

With -k, KILL is sent after the extra wait. Without -k, the process may stay alive after the warning.

More System and processes commands

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course