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.

8 minute lesson

~~~

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

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 and course as PDF and EPUB files.

Get the download library →