Bash

Run Command with Timeout

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`timeout` (GNU coreutils) kills a command after N seconds. Returns exit code 124 on timeout — let the caller distinguish "timed out" from "failed for other reasons."
Bash
Raw
# Plain — kill after 30 seconds
timeout 30s curl -s https://slow.api/data

# Force kill after grace period (default sends SIGTERM, then SIGKILL after 10s)
timeout --kill-after=10s 30s ./long-running-job.sh

# Distinguish timeout from other failures
if ! timeout 5s ping -c 100 example.com; then
    rc=$?
    if [[ $rc -eq 124 ]]; then
        echo "Timed out" >&2
    else
        echo "Failed with exit code $rc" >&2
    fi
fi
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.