Processes and jobs

Linux commands: nohup

Learn how the Linux nohup command keeps a process running after you log out or close the terminal, which is handy for long-lived jobs on a remote server.

Closing a terminal normally sends a hangup signal to jobs associated with that session. nohup starts a command with that signal ignored:

nohup ./generate-report >report.log 2>&1 &

Each part has a job:

  • nohup protects the command from the hangup signal.
  • >report.log records standard output.
  • 2>&1 sends errors to the same file.
  • & returns the shell prompt while the process runs in the background.

Without explicit redirection, many implementations write to nohup.out. Record the process ID immediately with echo $!, then verify the process and watch its log:

job_pid=$!
ps -p "$job_pid"
tail -f report.log

Stop the process later with kill "$job_pid". If you lose the process ID, find the process with ps before sending a signal. Also watch the log size: nohup.out and other unrotated logs can grow until they fill the filesystem.

Running a command with & alone only puts it in the background. It can still receive a hangup signal when the session closes. nohup and & solve two separate problems.

nohup is not a service manager. It does not restart a crashed process, start it after a reboot, rotate logs, manage dependencies, or expose structured status. Use systemd, a container orchestrator, or another supervisor for a production service. Use tmux or screen when you want to reconnect to an interactive terminal rather than detach a non-interactive job.

Also check whether the program needs input. A background command waiting on the closed terminal can still fail or stop.

Try it with nohup sh -c 'sleep 10; date' >result.log 2>&1 &, close or detach the session, then confirm the timestamp appears.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →