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.

LinuxsafetytimeoutcoreutilsscriptsLow risk
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.com

02What 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

RiskLow risk
RequirementsPart of GNU coreutils, preinstalled on all Linux distros
!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

Last verified: 2026-09-17 · Category: Developer