nohupKeep a command running after you log out
nohup runs a command immune to SIGHUP, the hangup signal sent when your terminal or SSH session goes away, and redirects its output to nohup.out if it was going to the terminal. Combined with &, it's the quickest way to start a long job that survives logout.
nohup COMMAND [ARG]... [> FILE 2>&1] &A data import takes two hours and you don't want to keep your laptop connected the whole time.
nohupSets SIGHUP to ignored, fixes up stdin/stdout/stderr if they are a terminal, then execs the command.
When a terminal goes away (you close the window, SSH drops, you log out with jobs still attached), the kernel sends SIGHUP to the session leader, your shell, and bash passes SIGHUP on to its jobs. The default action of SIGHUP is to terminate. nohup calls signal(SIGHUP, SIG_IGN) and then execs your command; an ignored signal stays ignored across exec, so the hangup has no effect. It also tidies the file descriptors: if stdin is a terminal it is replaced with an unreadable descriptor, if stdout is a terminal output goes to ./nohup.out (or $HOME/nohup.out if the current directory isn't writable), and if stderr is a terminal it follows stdout. After you log out, the process is orphaned and reparented to PID 1. nohup doesn't put the command in the background (that's &), and a program can undo it by installing its own SIGHUP handler.
When the session hangs up, SIGHUP travels from the terminal to bash and on to its jobs; nohup makes a job ignore it.
You SSH in. sshd gives bash a pseudo-terminal; bash is the session leader. You start one job with plain & and one with nohup ... &.
nohup CMD &Ignore SIGHUP and run in the background> file 2>&1Choose the output file instead of nohup.outnohup.outCreated in the current directory (or $HOME) when stdout is a terminalexit 127 / 126Command not found / found but not executabledisown -h %1Alternative for an already running job: bash won't send it SIGHUPsetsid CMDAlternative: run in a new session with no controlling terminalnohup ./long-job.sh without &nohup doesn't background anything; your prompt is blocked, and Ctrl-C (SIGINT, not SIGHUP) still kills the job.
nohup ./long-job.sh > job.log 2>&1 &Letting several jobs append to the same nohup.outOutput from unrelated jobs interleaves in one file that grows forever in whatever directory you were in.
Give each job its own log: > import.log 2>&1Using nohup to run a production serviceNothing restarts it when it crashes or the box reboots, there's no log rotation, and it's invisible to systemctl.
A systemd unit (Restart=on-failure), or systemd-run for one-offsRelying on nohup on hosts with systemd-logind KillUserProcesses=yeslogind kills every process in the user's session scope on logout, ignored SIGHUP or not.
systemd-run --user --unit=import ./import.sh, plus loginctl enable-lingerTry these on a Linux machine of your own. Each task has a hint and a solution.
Start `sleep 1000` with nohup in the background and check that its SIGHUP is ignored.
Run `nohup ls -l /etc` in your home directory, then find where the output went.
Start `sleep 600` with nohup in the background, logging stdout and stderr to ~/job.log.
Send SIGHUP to the nohup'd sleep and confirm it's still running. Then stop it properly.
nohupKeep a command running after you log out
nohup runs a command immune to SIGHUP, the hangup signal sent when your terminal or SSH session goes away, and redirects its output to nohup.out if it was going to the terminal. Combined with &, it's the quickest way to start a long job that survives logout.
nohup COMMAND [ARG]... [> FILE 2>&1] &A data import takes two hours and you don't want to keep your laptop connected the whole time.
nohupSets SIGHUP to ignored, fixes up stdin/stdout/stderr if they are a terminal, then execs the command.
When a terminal goes away (you close the window, SSH drops, you log out with jobs still attached), the kernel sends SIGHUP to the session leader, your shell, and bash passes SIGHUP on to its jobs. The default action of SIGHUP is to terminate. nohup calls signal(SIGHUP, SIG_IGN) and then execs your command; an ignored signal stays ignored across exec, so the hangup has no effect. It also tidies the file descriptors: if stdin is a terminal it is replaced with an unreadable descriptor, if stdout is a terminal output goes to ./nohup.out (or $HOME/nohup.out if the current directory isn't writable), and if stderr is a terminal it follows stdout. After you log out, the process is orphaned and reparented to PID 1. nohup doesn't put the command in the background (that's &), and a program can undo it by installing its own SIGHUP handler.
When the session hangs up, SIGHUP travels from the terminal to bash and on to its jobs; nohup makes a job ignore it.
You SSH in. sshd gives bash a pseudo-terminal; bash is the session leader. You start one job with plain & and one with nohup ... &.
nohup CMD &Ignore SIGHUP and run in the background> file 2>&1Choose the output file instead of nohup.outnohup.outCreated in the current directory (or $HOME) when stdout is a terminalexit 127 / 126Command not found / found but not executabledisown -h %1Alternative for an already running job: bash won't send it SIGHUPsetsid CMDAlternative: run in a new session with no controlling terminalnohup ./long-job.sh without &nohup doesn't background anything; your prompt is blocked, and Ctrl-C (SIGINT, not SIGHUP) still kills the job.
nohup ./long-job.sh > job.log 2>&1 &Letting several jobs append to the same nohup.outOutput from unrelated jobs interleaves in one file that grows forever in whatever directory you were in.
Give each job its own log: > import.log 2>&1Using nohup to run a production serviceNothing restarts it when it crashes or the box reboots, there's no log rotation, and it's invisible to systemctl.
A systemd unit (Restart=on-failure), or systemd-run for one-offsRelying on nohup on hosts with systemd-logind KillUserProcesses=yeslogind kills every process in the user's session scope on logout, ignored SIGHUP or not.
systemd-run --user --unit=import ./import.sh, plus loginctl enable-lingerTry these on a Linux machine of your own. Each task has a hint and a solution.
Start `sleep 1000` with nohup in the background and check that its SIGHUP is ignored.
Run `nohup ls -l /etc` in your home directory, then find where the output went.
Start `sleep 600` with nohup in the background, logging stdout and stderr to ~/job.log.
Send SIGHUP to the nohup'd sleep and confirm it's still running. Then stop it properly.