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).
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate