timeout Command on Linux: Limit How Long a Command Runs

timeout command linux - custom-timeout-featured.png

timeout on Linux (GNU coreutils) kills or signals a command after N seconds — searched for backup scripts that hang on NFS, runaway curl, or cron jobs that overlap.

Related: service watchdog,
cron,
kill & pkill.

Basic limit

Sends SIGTERM after duration, then SIGKILL if process ignores it.

bashbasic
timeout 10 sleep 60; echo exit=$?
timeout
duration

Exit code 124

Scripts can branch on timeout vs real failure.

bashcheck
if timeout 3 ping -c 100 8.8.8.8; then echo ok; else echo code=$?
124
exit

Grace period

-k waits extra seconds after SIGTERM before SIGKILL.

bashkill
timeout -k 5 30 some-long-job.sh
-k
SIGKILL

Preserve partial output

Pair with tee or log files before timeout ends the process.

Warningtimeout does not limit CPU of child daemons that re-exec — use cgroup/ systemd for long-running services.
cron
overlap

Quick Reference

  • Run: timeout 30s command
  • Timed out exit: 124
  • Grace: timeout -k 10 30 cmd

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.