When To Use Nohup Vs Disown For Long-Running Processes

The Core Difference Between nohup and disown

The Linux utilities nohup and disown both allow processes to continue running in the background after a user logs out or closes their terminal. However, nohup and disown achieve process persistence through different mechanisms:

  • nohup configures a process to ignore SIGHUP signals, which are sent to processes when a controlling terminal exits. This prevents the process from terminating upon shell exit.
  • disown removes a process from the shell’s job control. This detaches it from the controlling terminal so it keeps running independent of the shell session.

In essence, nohup makes a process immune to hangups while disown liberates a process from the shell’s control. This leads to some key differences:

  • nohup is applied when launching a process to make it ignore future SIGHUP signals.
  • disown is used on running jobs to detach them retroactively from the current shell.
  • nohup processes remain child processes of the launching shell.
  • disowned processes become child processes of init.

The choice between using nohup or disown depends on specific process persistence needs:

  • Use nohup when launching long-running background processes that should ignore SIGHUP.
  • Use disown when you want to detach existing running jobs from the current shell.
  • Combining nohup and disown offers the greatest process persistence.

Using nohup to Ignore HUP Signals

The SIGHUP signal is sent to processes when their controlling terminal closes or the parent process exits. This will terminate interactive shell jobs and background processes like daemons.

nohup is a POSIX command that causes processes to ignore SIGHUP signals when launched. It ensures the process keeps running in the background after exiting the session that started it.

To launch a long-running process with nohup, prepend the nohup command before issuing the operation:

$ nohup long_process &

Append ampersand (&) to run the process in the background. nohup will redirect the launched process’s standard output and standard error streams to a nohup.out file.

For example, this launches my_script.sh detached from the terminal using nohup:

$ nohup ./my_script.sh &> /dev/null & 

The script will keep running in the background after exiting the shell. nohup protects it from terminating on SIGHUP signals.

Leveraging disown to Detach Processes from Terminal Sessions

Shell job control allows interactive management of foreground and background processes. Jobs launched in the shell are attached to the terminal and will exit along with the shell.

The disown builtin command dissociates a running job from the shell’s job control. This prevents the process from receiving SIGHUP when the parent shell exits.

To disown a running job, use disown with the process ID or job number:

$ disown %1

Or to disown multiple specific jobs:

$ disown -h %1 %2 %4

This detaches jobs 1, 2, and 4 from the invoking terminal session. disown -h also removes the jobs from the shell’s job list.

To manually background all running jobs, use:

$ disown -a

After disowning jobs, you can log out safely without terminating those processes.

Persistence Limitations of nohup and disown

While nohup and disown help processes resist SIGHUP signals, other events can still cause early termination:

  • The parent init process ending forces all orphaned child processes to exit.
  • Killing the parent shell process will terminate child jobs launched with nohup.
  • System restarts terminate all running processes, including disowned jobs.

For fuller process persistence through reboots, additionally configure:

  • Init systems like systemd to restart processes on reboot.
  • Crontab @reboot jobs to relaunch processes.
  • Minecraft server wrappers to restart on system events.

Recommendations for Long-Running Process Management

nohup and disown provide simple builtin methods for process persistence in Linux shells. For launching long-running background jobs, applying nohup ensures process survival after hangups. For existing jobs needing independence from terminal sessions, disown removes their shell attachments.

For critical persistence, also utilize init systems or monitoring wrappers for process management across system restarts. Beware orphaned processes from poorly configured persistence that waste system resources.

Common recommendations include:

  • Use nohup when launching background daemons and batch jobs needing SIGHUP immunity.
  • Use disown for detaching interactive terminal jobs mid-session.
  • Combine nohup and disown for strongest process persistence.
  • Configure init systems to manage restarts on reboot.
  • Monitor background process behaviour with utilities like ps and top.
  • Implement log rotation, resource limits, and monitoring for orphaned processes.

Properly detaching jobs with nohup and disown enables walking away from terminals without disrupting long-running process execution. Combine with other process management methods for increased robustness across system events.

Leave a Reply

Your email address will not be published. Required fields are marked *