timeout Command
Intermediate Performance & Debugging man(1)Run a command with a time limit
👁 10 views
📅 Updated: Mar 15, 2026
SYNTAX
timeout [OPTION] DURATION COMMAND
What Does timeout Do?
timeout runs a command with a time limit. If the command does not complete within the specified duration, it is terminated. timeout is essential for preventing hung processes in scripts and automation.
timeout sends SIGTERM by default when the time limit expires, giving the process a chance to clean up. If the process does not exit, a SIGKILL can be sent with the --kill-after option.
timeout supports durations in seconds (default), minutes (m), hours (h), and days (d).
timeout sends SIGTERM by default when the time limit expires, giving the process a chance to clean up. If the process does not exit, a SIGKILL can be sent with the --kill-after option.
timeout supports durations in seconds (default), minutes (m), hours (h), and days (d).
Options & Flags
| Option | Description | Example |
|---|---|---|
| DURATION | Time limit | timeout 30 long_command |
| -s SIGNAL | Signal to send (default TERM) | timeout -s KILL 10 stuck_command |
| -k DURATION | Send KILL after additional time | timeout -k 5 30 command |
| --preserve-status | Exit with command status, not 124 | timeout --preserve-status 30 command |
Practical Examples
#1 30-second limit
Kills curl if it takes more than 30 seconds.
$ timeout 30 curl -s https://slow-api.example.com/data#2 With kill fallback
Sends SIGTERM at 60s, SIGKILL at 70s if still running.
$ timeout -k 10 60 ./long_process.sh#3 5-minute limit
Limits backup script to 5 minutes.
$ timeout 5m ./backup.sh#4 Test connectivity
Tests port connectivity with a 5-second timeout.
$ timeout 5 bash -c "echo > /dev/tcp/server/80" && echo "Open" || echo "Closed/Timeout"#5 In a loop
Pings each server with a 3-second timeout.
$ for host in $(cat servers.txt); do timeout 3 ping -c 1 $host > /dev/null && echo "$host: up" || echo "$host: down"; done#6 Check exit code
Exit code 124 means the command was killed by timeout.
$ timeout 10 sleep 30; [ $? -eq 124 ] && echo "Timed out"
Output:
Timed out
Tips & Best Practices
Exit code 124: timeout returns exit code 124 when it kills the command. Use this in scripts to detect timeouts.
-k for stubborn processes: Some processes ignore SIGTERM. Use -k 5 to send SIGKILL 5 seconds after SIGTERM if the process is still running.
Duration units: Default is seconds. Append s/m/h/d for seconds/minutes/hours/days: timeout 5m, timeout 2h.
Frequently Asked Questions
How do I limit how long a command runs?
timeout DURATION command. Example: timeout 30 ./script.sh kills the script after 30 seconds.
How do I detect if timeout killed the command?
timeout exits with code 124 when it kills the command. Check with $? in scripts.
What if the process ignores the timeout signal?
Use -k DURATION to send SIGKILL after the initial signal: timeout -k 5 30 command sends KILL 5 seconds after TERM.
Related Commands
More Performance & Debugging Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →