Interview Prep/Shell Scripting/Arguments, I/O, here-docs, process substitution and subshells

What does `timeout` do and how do you use it in scripts?

BeginnerConceptArguments, I/O, here-docs, process substitution and subshells
Answer

timeout DURATION cmd runs cmd and sends SIGTERM if it is still running after DURATION; it exits with 124 on timeout.

timeout 30s pg_isready -h db.internal || die "database not ready"
timeout -k 10s 5m ./migrate.sh     # SIGTERM at 5m, SIGKILL 10s later if still alive
rc=0; timeout 10 curl -fsS "$url" || rc=$?
(( rc == 124 )) && echo "timed out"

Every network call in an unattended script needs a timeout, either timeout or the tool's own (curl --max-time, ssh -o ConnectTimeout=5).

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arguments, I/O, here-docs, process substitution and subshells