Linux timeout Command
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
-
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)
-
Command:
- The command or program to execute
- Can be a built-in command or external program
-
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
timeout 5s ping example.com
# Terminate script execution after 1 minute
timeout 1m ./long_script.sh
Specifying Signal Type
Example
timeout -s INT 3s command
# Send KILL signal after 10 seconds (force termination)
timeout -s KILL 10s command
Combined Usage
Example
timeout -k 5s 10s command
Getting Exit Status
Example
timeout --preserve-status 5s command
echo $? # View the exit status code
Practical Application Scenarios
1. Prevent Infinite Script Execution
Example
timeout 2h ./backup_database.sh
2. Timeout Control in Automated Testing
Example
timeout 30s ./run_test_case.sh
3. Network Operation Timeout
Example
timeout 1m wget http://example.com/large_file.zip
4. Timeout Handling for Interactive Programs
Example
timeout 10s read -p "Please enter your choice: " choice
Notes
-
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
-
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 with
killthe command
-
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
-
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
timeout 30s command || echo "Command timed out, performing cleanup..."
2. Using in Scripts
Example
if timeout 10s command; then
echo "Command completed successfully"
else
echo "Command timed out or failed"
fi
3. Monitoring Resource Usage
Example
(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
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:
- Control program execution time to prevent resources from being occupied for long periods
- Improve script robustness and avoid infinite waiting
- Implement timeout mechanisms in automated tasks
- Handle operations with unpredictable execution times
By using the timeout command properly, you can make your Linux system administration more efficient and reliable.
Other Extensions
Linux Command Reference