Demystifying Background Processes: Nohup, Disown And & Explained

Keeping Processes Running After Terminal Closure

When a shell session is closed or a terminal window is exited, any processes started in that session are also terminated by default. However, often we want processes to continue running in the background even after closing the terminal. There are several methods in Linux to achieve this:

  • nohup – Detaches process from terminal and runs immune to hangups
  • disown – Removes process from job control
  • & – Forks process into background execution

These allow processes to keep running after their parent terminals are closed without being affected by the SIGHUP signal. This article will explain how each method works and when to use the appropriate backgrounding technique.

Detaching Processes with nohup

The nohup command detaches a process from the terminal session that started it, allowing it to keep running in the background after exiting the session. nohup stands for “no hangup” signaling the process to ignore hangup signals.

When a terminal sends the SIGHUP signal to notify processes of an impending hangup event, nohup causes the command to ignore this signal so execution continues in the background. This prevents the process from terminating on session exits.

Example nohup Command to Run Process in Background

To detach a process using nohup, prepend the command invocation with nohup:

$ nohup long_running_process &

This will start long_running_process, have it ignore hangup signals, and run it in the background. The standard output and stderr streams will be redirected to a nohup.out file.

Disowning Current Running Processes

The disown builtin command can detach a currently running job from the shell’s job control, allowing it to keep running after terminal exit without being killed. Job control allows the shell to stop, start, and monitor process jobs.

Disowning a running job removes it from the shell’s job control mechanism. This prevents the process from receiving SIGHUP when the parent shell exits, allowing it to continue running in the background.

Example disown Command to Detach Process from Terminal

To disown a running job and have it ignore hangups:

$ long_running_process
Ctrl + Z
$ bg 
$ disown

This stops the current foreground process, backgrounds it, then disowns it so it no longer tracked in the shell’s job list.

Forking Processes into the Background

Another way to background a process is to append an & ampersand symbol to the command invocation. This signals the shell to fork execution of that process to run in the background.

Forking makes the command a child process of the shell but detaches it into the background allowing the terminal to remain interactive while it runs.

Example Running Process in Background with & Operator

To background a process by forking it to the background:

$ long_running_process &

This will immediately background long_running_process allowing the shell to continue accepting input. Stdout can be redirected like a normal background job.

Monitoring Background Processes

When processes are running detached in the background, the ps command allows visibility into current processes from a system level.

ps displays snapshot info of running processes including background jobs, process ID, runtimes, command invocations, and resource utilization.

Example ps Command to View Background Processes

$ ps -ef
$ ps aux

Common options like aux and ef offer more verbose process listings helpful for inspecting background jobs.

Bringing Processes to Foreground

The fg command can bring a running background process back to the foreground terminal session. Job ID or process ID can identify processes.

This allows resuming interactive input/output with programs forked or disowned into the background non-interactively initially.

Example fg Command to Resume Process in Foreground

$ fg 567
$ fg %3

This resumes job 567 or job 3 in the foreground, enabling direct stdin/stdout access.

Ending Background Processes

Background processes can be killed or ended using the kill command and referencing either the PID or job ID much like foregrounding.

Kill sends a specified signal to the target process prompting it to terminate. SIGTERM is the default graceful shutdown signal.

Example kill command to terminate background process

$ kill %2
$ kill 1234  

Bash job specifiers or PID can target processes gracefully halting them even if disowned or nohupped.

Differences Between nohup, disown and &

Although nohup, disown, and & all background processes, there are some key differences:

nohup

  • Detaches process from terminal to prevent hangup termination
  • Output redirected to nohup.out by default
  • Ideal for longer running batch jobs to persist

disown

  • Removes job from shell’s job control
  • Useful for interactively backgrounding current fg processes
  • Already running processes can be disowned

&

  • Forks process into background execution
  • Easy way to immediately background from command line
  • Terminal remains interactive when used

When to Use Each Method for Backgrounding Processes

Determining which method to use depends on the specific process backgrounding needs:

  • nohup – Long running batch jobs needing to persist detached
  • disown – Interactively backgrounding foreground running jobs
  • & – One off background tasks while terminal remains usable

If a command will run for a long time non-interactively, nohup can allow it to continue immune to hangups. For short term or dynamic backgrounding of current terminal jobs, disown is helpful. If needing to quickly run one-off processes like compilation in the background, & is the simplest approach.

Benefits and Limitations of nohup vs disown vs &

Comparing the advantages and disadvantages of each method:

nohup Benefits

  • Persists processes past logout
  • Prevents SIGHUP hangup signal
  • Outputs to nohup.out by default

nohup Limitations

  • Cumbersome for short term backgrounding
  • Redirecting output can be complex

disown Benefits

  • Easily background currently running jobs
  • No output redirection needed
  • Simple job control detachment

disown Limitations

  • Parent session ending still terminates process
  • Only useful on running foreground tasks

& Benefits

  • Very simple syntax from command line
  • Immediately backgrounds without stopping jobs
  • Terminal remains usable

& Limitations

  • Parent shell exiting kills process
  • I/O redirection can be tricky

Leave a Reply

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