On this pageWhy closing SSH kills your job
You start a long import over SSH, your laptop sleeps, and the import is gone when you log back in. That is not a crash. It is Unix doing what it was designed to do when a terminal goes away. This guide explains the signal behind it, then covers the tools that keep a process alive, from a quick nohup to a proper systemd service, and when each one is the right call.
Why closing SSH kills your job
When the connection drops, the terminal session hangs up and the shell receives SIGHUP. Bash then resends SIGHUP to every job it started, running or stopped, before it exits. Most programs never handle that signal, so the default action applies: they terminate. If you type exit instead of losing the connection, an interactive login shell only sends SIGHUP to its jobs when the huponexit option is set, which is why background jobs sometimes survive and sometimes do not.
There is a second trap. A job that keeps writing to a terminal that no longer exists gets write errors, and many programs exit on the first one. Whatever method you pick, send output to a file.
Pick the right tool
| Method | Survives logout | Survives reboot | Reattach later | Good for |
|---|---|---|---|---|
nohup command & |
Yes | No | No | A one-off long command |
disown |
Yes | No | No | A job you already started without nohup |
setsid |
Yes | No | No | Fully detaching from the terminal |
tmux or screen |
Yes | No | Yes | Interactive work, installers, anything you want to watch |
systemd-run |
Yes | No | Logs in the journal | Ad hoc jobs with logging and resource limits |
| A systemd service | Yes | Yes | Logs in the journal | Anything that must keep running |
nohup: the quick fix
nohup runs a command with SIGHUP ignored. It does not put the command in the background; the trailing & does that.
nohup ./export.sh > export.log 2>&1 &
echo $!
The second line prints the process ID so you can find the job later. Without the redirection, nohup handles the terminal for you: if standard output is a terminal, output is appended to nohup.out in the current directory (or $HOME/nohup.out if that is not writable); standard error follows standard output; and if standard input is a terminal, it is replaced with an unreadable file. That is what the message nohup: ignoring input and appending output to 'nohup.out' means. GNU nohup has no options beyond --help and --version.
Common nohup errors
The exit status tells you what went wrong:
| Status | Meaning | Usual fix |
|---|---|---|
| 125 | nohup itself failed | Check the output file is writable |
| 126 | The command was found but could not run | chmod +x script.sh, or run nohup bash script.sh |
| 127 | The command was not found | Use a full path such as ./script.sh |
Three more mistakes come up often. Aliases and shell functions do not work after nohup, because nohup runs a program, not a shell. In a pipeline such as nohup cmd1 | cmd2 &, only cmd1 ignores the hangup. And sudo nohup ... in the background can sit forever waiting for a password prompt you cannot see. Wrap compound commands in a shell, and authenticate sudo first:
sudo -v
nohup sh -c 'pg_dump appdb | gzip > /backup/appdb.sql.gz' > dump.log 2>&1 &
Check on a background job
From the same shell, jobs -l lists your jobs. From a new SSH session, find the process by name or by the PID you saved:
pgrep -af export.sh
ps -o pid,ppid,stat,etime,cmd -p 4242
tail -f export.log
kill 4242
etime shows how long the job has run. kill sends SIGTERM, which lets the program clean up; use kill -9 only when it ignores that.
disown: rescue a job you already started
If the command is running in the foreground and you suddenly need to leave, suspend it with Ctrl+Z, resume it in the background, then remove it from the shell's hangup list:
bg
disown -h %1
disown -h keeps the job in the job table but stops the shell from sending it SIGHUP; plain disown %1 drops it from the table entirely. The catch: its output still points at your terminal, so a chatty program may die on its first write after you disconnect. Use this as a rescue, not a habit.
setsid: start in a new session
setsid runs a program in a new session with no controlling terminal, so a hangup never reaches it. With -f it always forks, and your prompt returns immediately:
setsid -f ./export.sh > export.log 2>&1 < /dev/null
tmux: when you need to come back
When you want to watch progress, answer a prompt or pick up where you left off, use a terminal multiplexer. The session lives on the server, independent of your connection:
tmux new -s import
Run your command, then detach with Ctrl+B followed by D. Later, tmux ls lists sessions and tmux attach -t import brings you back. screen works the same way: screen -S import, detach with Ctrl+A then D, and resume with screen -r import. This is the right tool for control panel installers and distribution upgrades, which ask questions halfway through.
systemd: for anything that matters
A transient job with systemd-run
systemd-run hands a command to the service manager, which runs it outside your login session, captures its output in the journal and lets you set limits:
sudo systemd-run --unit=export-job --property=Nice=10 /usr/local/bin/export.sh
journalctl -u export-job -f
systemctl status export-job
Running it as your own user with systemd-run --user also works, but the user manager stops when your last session ends unless lingering is enabled: sudo loginctl enable-linger "$USER".
A service unit
Anything that should restart after a crash or a reboot belongs in a unit file. Save this as /etc/systemd/system/worker.service:
[Unit]
Description=Queue worker
After=network-online.target
Wants=network-online.target
[Service]
User=app
WorkingDirectory=/srv/app
ExecStart=/srv/app/worker --queue default
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Then load and start it:
sudo systemctl daemon-reload
sudo systemctl enable --now worker.service
When even tmux dies at logout
systemd-logind has a setting, KillUserProcesses, that terminates every process in a login session when the user logs out, nohup and tmux included. Upstream systemd defaults it to yes; Debian and Ubuntu build it as no. Check the live value:
busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager KillUserProcesses
If it prints b true, use systemd-run as shown above, which places the job outside your session, or set KillUserProcesses=no in a drop-in file under /etc/systemd/logind.conf.d/. Root is excluded by default.
FAQ
Does nohup survive a reboot?
No. A reboot stops every process. If the job must come back on its own, write a systemd service with WantedBy=multi-user.target and enable it.
Is a plain & enough on its own?
Sometimes. A background job started with & survives a clean exit from a shell without huponexit, but not a dropped connection, because the shell forwards SIGHUP when the terminal hangs up. Adding nohup removes the guesswork.
Do I need nohup in cron jobs?
No. Cron runs commands without a terminal, so there is no hangup to ignore. Redirect the output to a log file instead of letting cron mail it.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




