Run a command with a time limit using `timeout`
The coreutils command that keeps cron jobs and scripts from hanging forever — with suffix syntax and signal options almost nobody remembers.
Run `timeout 30s command` to kill it after 30 seconds; use `timeout -k 5s 30s command` to SIGKILL 5 seconds later if it ignores the first signal.
01Command
Command
$ timeout 30s ping -c 100 example.com02What it does
Durations accept plain numbers (seconds) or suffixes: `500ms`, `30s`, `10m`, `2h`. timeout sends SIGTERM first; some programs trap it, which is what the forgotten `-k` (kill-after) covers — `timeout -k 5s 30s rsync ...` gives the process a 5-second grace period before SIGKILL. Also handy in scripts: `--preserve-status` makes the exit code reflect the child's, and `-s INT` sends SIGINT instead for programs that treat Ctrl+C specially.
03Risk
!Warning: By default timeout returns exit code 124 when it kills the command — check for that specifically in scripts to distinguish 'timed out' from real failures.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- timeout(1) — run a command with a time limitEvidence: Documents DURATION suffixes (s/m/h), -k KILL after-time, -s signal selection, --preserve-status and -v
Last verified: 2026-09-17 · Category: Developer