Three small commands, each wrapping another command, each about what happens to a process when the thing that started it goes away. timeout runs a command and kills it if it is still going after a deadline — the one-line fix for a health check, backup or API call that sometimes never returns. nohup runs a command immune to the hangup signal a terminal sends when it closes, so a job survives your SSH session ending. setsid runs a command in a new session with no controlling terminal at all, which is the stronger form of the same idea and the one daemons use. People reach for them in the wrong order and in the wrong combinations more than for almost any other commands, usually because the problem they are solving — the one where ssh host 'cmd &' hangs — is not the problem any of the three was built for.
timeout 30 curl -sf https://example.com/health
nohup ./long-job.sh > job.log 2>&1 &
setsid -f ./daemon-like-thing > /dev/null 2>&1 < /dev/nullThe first gives curl thirty seconds and then sends it SIGTERM. The second starts a job that ignores hangups, with its output sent somewhere that is not the terminal, in the background. The third starts a program in its own session, forked so the shell gets its prompt back, with all three standard streams pointed away from the terminal — which is the combination that actually detaches a process, and the reason it needs all of those pieces is the whole second half of this page. First, timeout, whose exit codes are the thing to memorise:
$ timeout 1 sleep 5; echo "rc=$?"
rc=124
$ timeout --preserve-status 1 sleep 5; echo "rc=$?"
rc=143
$ timeout 1 true; echo "rc=$?"
rc=0
$ timeout 1 false; echo "rc=$?"
rc=1
$ timeout -s KILL 1 sleep 5; echo "rc=$?"
rc=137124 means “I had to stop it”; any other status is the command’s own, so a script can tell a slow health check from a failing one. --preserve-status swaps 124 for the status the command died with (143 = 128 + SIGTERM), which some test harnesses prefer. -s picks the signal, and KILL gives 137 — but sending KILL first is almost never right, because the command gets no chance to clean up. The right pattern for a command that might ignore TERM is below.
The timeout that does not stop anything
timeout sends one signal at the deadline and then waits for the command to exit. If the command catches or ignores SIGTERM — and shells, interpreters and anything with a cleanup handler often do — it keeps running to completion, and timeout reports 124 only after it has finished on its own. Without -k, a timeout is a polite request. Measured, with a script that traps TERM and runs for ten seconds:
$ cat ignore.sh
#!/bin/bash
trap 'echo "got TERM, ignoring" >&2' TERM
for i in $(seq 1 10); do sleep 1; done
echo "finished normally"
$ time (timeout 1 ./ignore.sh; echo "rc=$?")
Terminated
got TERM, ignoring
finished normally
rc=124
real 0m10.023s
$ time (timeout -k 1 1 ./ignore.sh; echo "rc=$?")
got TERM, ignoring
Killed
rc=137
real 0m2.002sWith a one-second limit the script announced the signal, ignored it, ran the full ten seconds and printed “finished normally” — and timeout then said 124, as if it had done something. With -k 1, the TERM at one second was followed by a KILL one second later, which nothing can ignore: two seconds total, status 137. Use -k whenever the deadline matters more than the cleanup — timeout -k 10 60 cmd gives the command a minute, then ten seconds to shut down gracefully, then kills it. The other thing the output shows is that timeout signals the command’s whole process group, not just the command: the inner sleep 1 died too (that first Terminated line), and the loop simply started the next one. The section on --foreground covers when that is and is not what you want.
Options
| Command and flag | Meaning |
|---|---|
timeout DURATION CMD | Run CMD; after DURATION (seconds, or 30s, 5m, 2h, 1d, fractions allowed), send it SIGTERM. Exit 124 if the deadline hit, else the command’s status |
timeout -k DUR2 | If still running DUR2 after the first signal, send SIGKILL (exit 137) |
timeout -s SIG | Signal to send at the deadline instead of TERM (INT, HUP, KILL, a number) |
timeout --preserve-status | Exit with the command’s own status even when the deadline hit (143 for TERM), instead of 124 |
timeout --foreground | Run the command in timeout‘s own process group: it can read the terminal and receive its signals, but only the command itself is signalled, not its children |
timeout -v | Say on stderr which signal was sent and when |
nohup CMD [ARGS] | Run with SIGHUP ignored. If stdout is a terminal, append it to ./nohup.out (or $HOME/nohup.out); if stderr is a terminal, send it to stdout; if stdin is a terminal, read from nowhere. Streams that are already files or pipes are left alone |
setsid CMD | Run in a new session and process group, with no controlling terminal. If setsid is already a group leader it forks first |
setsid -f / --fork | Always fork, returning at once; the command is now a child of nothing you own |
setsid -w / --wait | With -f, wait for the command and exit with its status |
setsid -c / --ctty | Make the current terminal the controlling terminal of the new session (for building things like script or a login) |
Why nohup is not the fix for “ssh hangs”
The commonest reason to reach for nohup is a command that will not let go: ssh host 'service start &' never returns, or a deploy script’s last line starts a server in the background and the CI job hangs for an hour. That is not a hangup problem at all. It is a file descriptor problem: the backgrounded process inherited the session’s stdout and stderr, and the session cannot end while something still holds its output pipe open. The same mechanism, reproduced with a pipe instead of SSH:
$ time (bash -c 'sleep 3 &' | cat) # the pipe stays open until sleep exits
real 0m3.007s
$ time (bash -c 'sleep 3 >/dev/null 2>&1 &' | cat) # output detached: returns at once
real 0m0.004s
$ time (bash -c 'nohup sleep 3 &' | cat) # nohup alone: still 3 s
real 0m3.005s
$ time (bash -c 'setsid sleep 3 &' | cat) # setsid alone: still 3 s
real 0m3.009sNeither nohup nor setsid helps, because neither touches where the output goes when it is already a pipe — nohup only redirects streams that are terminals. The fix is the redirection: >/dev/null 2>&1 </dev/null (or to a log file), and then the job is holding nothing the session needs. Once the streams are detached, nohup earns its place for the other problem: an interactive terminal closing sends SIGHUP to its jobs, and a job started under nohup ignores it. Measured under a real pseudo-terminal:
$ nohup sleep 30 &
nohup: appending output to 'nohup.out'
$ ps -o pid,pgid,sid,tty,ignored,cmd -p $!
PID PGID SID TT IGNORED CMD
788 787 787 pts/0 0000000000000007 sleep 30
$ kill -HUP 788; ps -p 788 -o pid,cmd
PID CMD
788 sleep 30
$ sleep 31 & kill -HUP $!
Hangup sleep 31The nohup‘d process shows HUP in its ignored-signal mask (bit 1 of 0x7) and survives one; the plain one dies. Note what did not change: the process is still on pts/0, in the shell’s session and process group. It ignores the terminal going away but it has not left the terminal, which is the difference between nohup and setsid.
setsid: actually leaving
$ setsid sleep 32 &
$ ps -o pid,pgid,sid,tty,cmd -p $(pgrep -f 'sleep 32')
PID PGID SID TT CMD
807 807 807 ? sleep 32
$ setsid -f sleep 0.2; echo "returned, rc=$?"
returned, rc=0
$ setsid -w sh -c 'exit 7'; echo "rc=$?"
rc=7Under setsid the process is its own session leader (SID equals its PID), its own group leader, and has no terminal at all (TT ?): nothing a terminal does can reach it, and SIGHUP never comes because there is nothing to hang up. This is what a daemon does to itself at startup, and setsid -f cmd >/dev/null 2>&1 </dev/null is the complete one-line “start this and walk away” — the fork so the shell returns, the new session so no terminal owns it, the redirections so no pipe does. -w is the opposite tool for a different job: run something in a fresh session but still wait for its result, which is how to stop a child from grabbing or being killed through your terminal while keeping its exit status.
Two things neither command protects against. On a desktop, systemd-logind may kill every process in your session when you log out, setsid or not, if KillUserProcesses=yes is set — the answer there is systemd-run --user --scope or a proper user service. And on a server, a job that must survive reboots or be restarted on failure should be a systemd unit, not a setsid: the three commands on this page are for the one-off, not the service. For an interactive session you intend to come back to, tmux does all of this and keeps the terminal, which is usually what you actually wanted.
timeout and the process group
By default timeout puts the command in its own process group and signals the group, so a script’s children die with it. --foreground changes that — the command stays in timeout‘s group, can read the terminal and be interrupted with Ctrl-C, but only the command itself is signalled. Measured with a script whose only job is to start a thirty-second child and wait for it:
$ timeout 1 ./parent.sh; echo "rc=$?"; pgrep -f 'sleep 30' || echo "no orphans"
rc=124
no orphans
$ timeout --foreground 1 ./parent.sh; echo "rc=$?"; pgrep -f 'sleep 30'
rc=124
891Without --foreground the whole group went; with it, the parent script died at one second and its sleep 30 ran on, orphaned, for the other twenty-nine. So --foreground is for an interactive command you want to be able to interrupt by hand — and when the command spawns helpers, it is the flag that leaves them behind.
Common problems
| Symptom | Cause | Check / fix |
|---|---|---|
timeout reports 124 but the command ran to completion | Command ignored or trapped TERM | timeout -k 5 … |
Script hangs at the end, or ssh host 'cmd &' never returns | Background job holds the session’s stdout/stderr | Redirect all three streams: cmd >/dev/null 2>&1 </dev/null & |
Job dies when the terminal closes despite & | Shell sent SIGHUP to its jobs | nohup cmd &, or disown after starting it, or setsid -f |
nohup.out appears in random directories | stdout was the terminal when nohup ran | Always redirect: nohup cmd > log 2>&1 & |
| Job dies on logout anyway | logind KillUserProcesses=yes, or a cgroup teardown | systemd-run --user --scope cmd, or a user service; loginctl show-session |
setsid returned at once and the status is lost | It forked because the caller was a group leader (interactive shell) | setsid -w when you need the status; -f when you explicitly do not |
timeout --foreground leaves children running | Only the direct child is signalled | Drop --foreground, or have the script trap TERM and kill its own children |
timeout on a pipeline times only the first command | timeout 5 a | b wraps a | timeout 5 sh -c 'a | b' |
| Exit 124 but the command printed its own error | Command exited with 124 itself (rare, but curl and others use it) | --preserve-status, or check stderr; 124 is only reliable as “timed out” when the command never uses it |
| Deadline in minutes refused | An older BusyBox timeout accepts plain seconds only | timeout 300 rather than 5m on Alpine |
Quick reference
timeout 30 cmd # TERM after 30 s; exit 124 if it fired
timeout -k 10 60 cmd # TERM at 60 s, KILL 10 s later if still alive
timeout -s INT 5m cmd # a different signal; m/h/d suffixes
timeout --preserve-status 10 cmd # exit with cmd's status (143) instead of 124
timeout --foreground 10 cmd # interactive: Ctrl-C works, children not signalled
timeout 5 sh -c 'a | b' # time a whole pipeline
nohup cmd > cmd.log 2>&1 & # survive the terminal closing, log explicitly
nohup cmd >/dev/null 2>&1 </dev/null & # ...and hold no stream the session needs
setsid -f cmd >/dev/null 2>&1 </dev/null # fully detached: new session, no tty, no pipes
setsid -w cmd # new session, but wait and return its status
disown -h %1 # bash: stop sending HUP to a job already started
systemd-run --user --scope cmd # survive logout under logind
ps -o pid,pgid,sid,tty,ignored,cmd -p PID # who owns it: group, session, terminal, ignored signalsRelated reading
- What a Terminal Actually Is — the long one on sessions, process groups, controlling terminals and where
SIGHUPcomes from - The Shell, in Depth — background jobs,
disown, and what&does and does not detach - tmux — the usual right answer for a long interactive job
- flock —
timeout‘s companion for cron jobs: one copy at a time, and never a copy that runs forever - systemd timers —
RuntimeMaxSec=andTimeoutStopSec=, the service-unit forms of this page - The Life of an SSH Connection — the session that ends, and what it takes with it

