Linux timeout Command

Linux 命令大全Linux Command Reference


timeout is a practical command in Linux systems, used to terminate running commands after a specified time. This command is especially suitable for the following scenarios:

  • Limit long-running processes
  • Prevent scripts or programs from executing indefinitely
  • Set execution time limits for critical operations
  • Timeout control in automated testing

The timeout command is part of the GNU coreutils package and is pre-installed in most Linux distributions.


Command Syntax

The basic syntax format of the timeout command is as follows:

timeout [选项] 持续时间 命令 [参数...]

Parameter Description

  1. Duration:

    • Can be a pure number (default unit is seconds)
    • Can also specify a unit:10s(10 seconds),5m(5 minutes),1h(1 hour)
    • Supports decimals:0.5s(500 milliseconds),1.5m(1 minute 30 seconds)
  2. Command:

    • The command or program to execute
    • Can be a built-in command or external program
  3. Parameters:

    • Arguments passed to the command

Common Options

Option Description Example
-sor--signal Specify the signal sent after timeout (default is TERM) timeout -s KILL 5s command
-kor--kill-after If the initial signal is ineffective, send KILL signal after the specified time timeout -k 5s 10s command
--preserve-status Return the exit status of the terminated command timeout --preserve-status 5s command
--foreground Run the command in the foreground (default is background) timeout --foreground 5s command
-vor--verbose Show verbose output timeout -v 5s command

Usage Examples

Basic Usage

Example

# Terminate the ping command after 5 seconds
timeout 5s ping example.com

# Terminate script execution after 1 minute
timeout 1m ./long_script.sh

Specifying Signal Type

Example

# Send INT signal after 3 seconds (equivalent to Ctrl+C)
timeout -s INT 3s command

# Send KILL signal after 10 seconds (force termination)
timeout -s KILL 10s command

Combined Usage

Example

# Send TERM signal first, then send KILL signal after 5 seconds
timeout -k 5s 10s command

Getting Exit Status

Example

# Preserve the exit status of the terminated command
timeout --preserve-status 5s command
echo $?  # View the exit status code

Practical Application Scenarios

1. Prevent Infinite Script Execution

Example

# Limit the database backup script to run for at most 2 hours
timeout 2h ./backup_database.sh

2. Timeout Control in Automated Testing

Example

# Let the test case run for at most 30 seconds
timeout 30s ./run_test_case.sh

3. Network Operation Timeout

Example

# Try downloading the file for at most 1 minute
timeout 1m wget http://example.com/large_file.zip

4. Timeout Handling for Interactive Programs

Example

# Give the user at most 10 seconds to enter a choice
timeout 10s read -p "Please enter your choice: " choice

Notes

  1. Signal Handling:

    • Some programs may catch and ignore the TERM signal; in that case, the KILL signal is needed
    • Use the KILL signal with caution during important data operations, as it may cause data corruption
  2. Child Process Handling:

    • By default, timeout only terminates the process it directly started, not necessarily all child processes
    • To terminate the entire process tree, consider combining withkillthe command
  3. Time Precision:

    • The time precision of the Linux kernel is usually at the millisecond level
    • Very short durations (such as 0.001s) may not be precisely controlled
  4. Return Values:

    • If the command completes before timeout, the command's exit status is returned
    • If terminated due to timeout, returns 124
    • If timeout itself encounters an error, returns 125-128

Advanced Tips

1. Combining with Other Commands

Example

# Perform cleanup operations after timeout
timeout 30s command || echo "Command timed out, performing cleanup..."

2. Using in Scripts

Example

#!/bin/bash

if timeout 10s command; then
    echo "Command completed successfully"
else
    echo "Command timed out or failed"
fi

3. Monitoring Resource Usage

Example

# Limit CPU time (requires combining with ulimit)
(ulimit -t 10; timeout 20s command)

FAQ

Q1: What is the difference between timeout and sleep?

A: sleep simply waits for a specified time, while timeout limits the execution time of another command.

Q2: How to terminate all child processes started by timeout?

A: You can use the process group method:

timeout --foreground 10s sh -c 'command & wait'

Q3: Why doesn't my program terminate immediately after timeout?

A: It may be because the program caught the default TERM signal; try using the-s KILLoption.

Q4: How to determine if a command was terminated due to timeout?

A: Check whether the exit status code is 124:

Example

timeout 5s slow_command
if [ $? -eq 124 ]; then
    echo "Command terminated due to timeout"
fi

Summary

The timeout command is a very useful tool in Linux system administration; it can help us:

  1. Control program execution time to prevent resources from being occupied for long periods
  2. Improve script robustness and avoid infinite waiting
  3. Implement timeout mechanisms in automated tasks
  4. Handle operations with unpredictable execution times

By using the timeout command properly, you can make your Linux system administration more efficient and reliable.


Linux 命令大全Linux Command Reference

Other Extensions