Timeout Command in Linux

By 

•

Updated on

•

9 min read

Stopwatch icon above the words timeout command

The timeout command runs a specified command and terminates it if it is still running after a given period of time. It is part of the coreutils package, which is installed on virtually every Linux distribution. Ubuntu 26.04 ships the Rust-based uutils version of timeout with the same options.

This command is useful when you need to enforce a time limit on a command that does not have a built-in timeout option, such as tail -f, a backup job, or a long-running script.

This guide explains how to use the timeout command with practical examples.

Syntax

The general syntax of the timeout command is:

txt
timeout [OPTIONS] DURATION COMMAND [ARG]...

The DURATION can be a positive integer or a floating-point number, followed by an optional unit suffix:

  • s - seconds (default)
  • m - minutes
  • h - hours
  • d - days

When no unit is specified, the duration defaults to seconds. If the duration is set to zero, the timeout is disabled.

Basic Usage

To terminate the ping command after five seconds:

Terminal
timeout 5 ping 8.8.8.8

To set a timeout of five minutes:

Terminal
timeout 5m ping 8.8.8.8

You can also use floating-point values. The following example sets a timeout of 1.5 minutes (90 seconds):

Terminal
timeout 1.5m ping 8.8.8.8

If the command requires elevated privileges, prepend sudo before timeout:

Terminal
sudo timeout 300 tcpdump -n -w data.pcap

Sending a Specific Signal

By default, timeout sends the SIGTERM signal to the command when the time limit is reached. You can specify a different signal using the -s (--signal) option.

For example, to send SIGKILL to ping after one minute:

Terminal
timeout -s SIGKILL 1m ping 8.8.8.8

You can also use the signal number instead of the name. The following command is equivalent:

Terminal
timeout -s 9 1m ping 8.8.8.8

When GNU timeout sends SIGKILL, the exit status is 137.

Terminal
timeout -s KILL 2 sleep 5
echo $?
output
Killed
137

The Killed line comes from the shell. In this example, GNU timeout sends the signal to its whole process group, including itself. The value 137 is 128 plus 9, the signal number of SIGKILL. With uutils 0.10.0 on Ubuntu 26.04, the same example prints only 124.

To list all available signals, use the kill -l command:

Terminal
kill -l

Killing Stuck Processes

Some processes catch or ignore the SIGTERM signal, which means they continue running after the initial termination signal is sent. To ensure the process is killed, use the -k (--kill-after) option followed by a time period.

When the time limit is reached, timeout sends SIGTERM by default or the signal you selected with -s. If the process is still running after the -k delay, it sends SIGKILL, which cannot be caught or ignored.

In the following example, timeout runs the command for one minute. If the process does not terminate after receiving SIGTERM, it is forcefully killed 10 seconds later:

Terminal
timeout -k 10 1m ping 8.8.8.8

If timeout has to send SIGKILL after the grace period, you get exit status 137.

Verbose Output

To see when timeout sends a signal, use the -v (--verbose) option. It prints one line to standard error for each signal it sends:

Terminal
timeout --verbose 2 sleep 10
output
timeout: sending signal TERM to command 'sleep'

The line names the signal (TERM) and the command that received it (sleep). If you combine --verbose with -k and the command ignores SIGTERM, a second line reports the KILL signal. In cron jobs and CI logs, this line tells you that a failure was caused by the time limit.

Preserving the Exit Status

By default, timeout returns 124 after the time limit expires and the command exits. GNU timeout returns 137 when it sends SIGKILL. If the command finishes before the time limit, timeout passes through the command’s exit status , which can also be 124 or 137.

You can check the exit code with echo $? right after the command:

Terminal
timeout 2 sleep 5
echo $?
output
124

The sleep command did not finish within two seconds. timeout stopped it and returned 124.

To return the exit status of the command even when a timeout occurs, use the --preserve-status option:

Terminal
timeout --preserve-status 2 sleep 5
echo $?
output
143

The value 143 is 128 plus 15, the signal number of SIGTERM. When a process is terminated by a signal, the shell reports its exit status as 128 plus the signal number.

On Ubuntu 26.04 with uutils 0.10.0, combining --preserve-status with -k can return the raw signal number . If SIGTERM stops sleep during the grace period, uutils returns 15 and GNU timeout returns 143.

Running in Foreground

When you run timeout from a script attached to a terminal, the new process group can be in the background. If the command tries to read from the terminal, the terminal sends SIGTTIN and stops the command. An interactive shell with job control usually puts commands you run directly at the prompt in the terminal’s foreground process group.

If your script is already in the terminal’s foreground, use --foreground to let the timed command read from the terminal.

Terminal
timeout --foreground 5m ./script.sh

This is useful for prompts and editors launched by a script. In this mode, timeout signals only the command itself. Its child processes can keep running after the time limit.

With uutils 0.10.0 on Ubuntu 26.04, an attempted terminal read from the background group can also stop timeout itself. The script can then hang past the time limit. GNU timeout keeps running and can still enforce the limit in this situation.

Using timeout in Scripts

The timeout command is commonly used in shell scripts to prevent commands from hanging indefinitely. Check the exit code to determine whether the command completed or was terminated:

sh
#!/bin/bash

if timeout 30 curl -fs -o /tmp/data.json https://api.example.com/data; then
    echo "Download completed successfully."
else
    if [ $? -eq 124 ]; then
        echo "Download timed out after 30 seconds."
    else
        echo "Download failed."
    fi
fi

Inside the else branch, $? still holds the exit status of timeout. In this example, 124 is the expected status when timeout stops curl after 30 seconds. The -f option makes curl fail on HTTP errors such as 404. Without it, you would see a missing page reported as a successful download.

Using timeout with Pipelines

The timeout command applies only to the single command that follows it, not to an entire pipeline. To apply a timeout to a pipeline, wrap it in bash -c:

Terminal
timeout 30 bash -c 'curl -s https://example.com | grep "pattern"'

Without the bash -c wrapper, timeout limits only curl. When curl is terminated, grep reaches the end of its input and exits. A slow command later in the pipeline still has no time limit. With the wrapper, GNU timeout sends the signal to Bash and the pipeline stages that are still in its process group.

On Ubuntu 26.04 with uutils 0.10.0, SIGKILL can leave pipeline stages running. This applies both to -s KILL and to escalation with -k. To use GNU’s group signaling on Ubuntu, run the command with gnutimeout.

Quick Reference

TaskCommand
Run command with 5-second timeouttimeout 5 COMMAND
Run command with 5-minute timeouttimeout 5m COMMAND
Send SIGKILL instead of SIGTERMtimeout -s SIGKILL 1m COMMAND
Kill if SIGTERM is ignored (10s grace)timeout -k 10 1m COMMAND
Print a message when a signal is senttimeout --verbose 5 COMMAND
Preserve command exit status on timeouttimeout --preserve-status 5 COMMAND
Run interactive command with timeouttimeout --foreground 5m COMMAND
Timeout a pipelinetimeout 30 bash -c 'cmd1 | cmd2'

FAQ

What exit code does timeout return?
A timeout normally produces 124. With --preserve-status, the result reflects how the command exited. GNU timeout reports 137 when it sends SIGKILL. An internal timeout error produces 125. If the command is found but cannot be started, timeout returns 126. If the command is missing, it returns 127.

What is the difference between SIGTERM and SIGKILL?
SIGTERM (signal 15) asks the process to terminate gracefully. The process can catch it and clean up before exiting. SIGKILL (signal 9) forcefully terminates the process immediately and cannot be caught or ignored.

Can I use timeout with sudo?
Yes. Place sudo before timeout: sudo timeout 60 tcpdump -n -w capture.pcap. With the default SIGTERM, the reverse order also works. sudo passes SIGTERM on to the command it started, but SIGKILL cannot be passed on. If you put timeout -s KILL or timeout -k before sudo, only sudo is killed, and the privileged command keeps running.

Does timeout work with shell built-in commands?
Not directly. timeout runs external commands, while shell built-ins such as cd, read, and ulimit need a shell context. To apply a timeout to a built-in, wrap it with bash -c. For reading input, the built-in -t option of read is a better fit: read -t 5 -p "Input: " val. A variable set inside bash -c is lost when the child shell exits.

How do I timeout a background process?
Use timeout directly: timeout 60 ./long-task.sh &. The timeout process runs in the background and manages the child command. Check its status with wait or jobs.

Troubleshooting

timeout: command not found
Install GNU coreutils. On most Linux distributions, timeout is included by default, but minimal containers or stripped-down environments may not include it.

timeout: failed to run command
timeout could not start the command. Exit code 127 means the command was not found, and shell built-ins such as cd fail the same way. Exit code 126 means the file exists but is not executable. On Ubuntu 26.04, the message reads timeout: failed to execute process: No such file or directory (os error 2).

Command exits with code 124 unexpectedly
The time limit may have expired, or the command may have returned 124 itself. Run it with --verbose and look for a signal message before increasing the time limit.

Exit code 137 instead of 124
GNU timeout returns 137 when it sends SIGKILL, either with -s KILL or after the -k grace period. The same status can come from a SIGKILL sent by another process or from the command’s own exit code. Use --verbose to check whether timeout sent the signal.

Process does not stop after timeout
Some processes ignore SIGTERM. Use -k to enforce a kill after a grace period: timeout -k 10 1m COMMAND.

Timeout does not apply to the whole pipeline
Wrap the full pipeline with timeout 30 bash -c 'cmd1 | cmd2'. If pipeline stages survive SIGKILL with uutils 0.10.0, use gnutimeout on Ubuntu.

Conclusion

Before relying on timeout in an unattended job, check how the command handles SIGTERM and whether any child processes keep running.

For more on signals and how to send them by hand, see our guide on the kill command .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page