timeout

process managementLinux
The timeout command is one of the most frequently used commands in Linux/Unix-like operating systems. timeout Run a command with a time limit

Quick Reference

Command Name:

timeout

Category:

process management

Platform:

Linux

Basic Usage:

timeout [options] [arguments]

Common Use Cases

    Syntax

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

    Options

    Option Description
    --preserve-status Exit with the same status as COMMAND, even when the command times out
    --foreground When not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals
    -k, --kill-after=DURATION Send KILL signal if COMMAND is still running after DURATION from sending the initial signal
    -s, --signal=SIGNAL Specify the signal to be sent on timeout (SIGTERM by default)
    --help Display help information and exit
    --version Output version information and exit
    DURATION Format
    DURATION is a positive integer or decimal number with an optional suffix:
    s Seconds (default)
    m Minutes
    h Hours
    d Days

    Examples

    How to Use These Examples

    The examples below show common ways to use the timeout command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Run 'sleep 10' but terminate it after 5 seconds timeout 5 sleep 10
    # Run a command with decimal time (2.5 seconds) timeout 2.5 sleep 10
    # Run a command in the background with a timeout timeout 30 command &
    # Advanced Examples Advanced
    # Send a specific signal (SIGTERM) when time expires timeout -s TERM 10 command
    # Kill the command if it's still running after sending the signal timeout -k 5 10 command # Use different time units (minutes) timeout 2m command # Use different time units (hours) timeout 1h command # Preserve the exit status of the command timeout --preserve-status 10 command # Send a different signal (SIGKILL) when time expires timeout -s KILL 10 command # Force timeout to run command using a new process group timeout --foreground 10 command # Handle verbose output with timeout timeout 10 command | grep "pattern" # Use timeout in a shell script to prevent hanging timeout 60 ./potentially_hanging_script.sh || echo "Script timed out" # Timeout a network connection test timeout 5 ping -c 10 example.com # Limit time for a large file operation timeout 30 cp large_file.iso /destination/

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `timeout` command is a utility that runs another command with a time limit. If the specified command is still running after the timeout period, it is sent a signal (by default, SIGTERM) to terminate it. This makes `timeout` a valuable tool for preventing processes from running indefinitely, which could potentially consume system resources or cause other issues. The basic syntax is straightforward: `timeout DURATION COMMAND [ARGUMENTS]`, where DURATION specifies how long the command is allowed to run before being terminated. The duration can be specified as a number followed by an optional unit suffix (s for seconds, m for minutes, h for hours, d for days). If no suffix is provided, seconds are assumed. By default, `timeout` sends the SIGTERM signal to the running command when the time limit is reached. However, this can be changed using the `-s` or `--signal` option to specify a different signal. Common alternatives include SIGKILL (which cannot be caught or ignored by the process) or SIGHUP. One particularly useful feature is the `-k` or `--kill-after` option. This specifies a second duration after which a SIGKILL will be sent if the command is still running after the initial signal. This helps handle cases where a program might catch or ignore the first termination signal. The `--preserve-status` option makes `timeout` exit with the same status as the command, even when the command times out. Without this option, `timeout` exits with status 124 if the command times out, which can be useful for detecting timeout conditions in scripts. The `--foreground` option is helpful when running `timeout` in environments where it's not directly connected to a terminal. It ensures that the command can still read from the terminal and receive TTY signals. The `timeout` command is particularly useful in scripts for tasks like: - Network operations that might hang due to connectivity issues - Commands that occasionally freeze or enter infinite loops - Limiting the runtime of resource-intensive operations - Implementing watchdog functionality to ensure processes don't run too long It's important to note that `timeout` works by sending signals to terminate the process. If the process traps or ignores these signals, it might not terminate as expected. In such cases, using SIGKILL (`-s KILL`) can help, as this signal cannot be caught or ignored by the process.

    Related Commands

    These commands are frequently used alongside timeout or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the timeout command works in different scenarios.

    $ timeout
    View All Commands