ash Nohup vs Ampersand

Dr. Muhammad Abdullah Feb 02, 2024
  1. Run Linux Process in the Background
  2. the Ampersand (&) Control Operator
  3. the nohup Command
  4. Difference Between Control Operator & and nohup Command
ash Nohup vs Ampersand

This short article explains the nohup command and the & control operator to run Linux processes in the background through Bash. Moreover, we will further look into the main differences between nohup and the &.

Run Linux Process in the Background

Linux provides two methods to run processes or commands in the background.

  1. Use the ampersand (&) control operator exclusively.
  2. Use the nohup command in conjunction with &.

the Ampersand (&) Control Operator

We can run any command asynchronously in the background using the & control operator.

Consider the following command.

sleep 10

The sleep command adds a delay of a specific time. When we run the sleep 10 command, it suspends the Bash terminal for 10 seconds, and we cannot run any other command on the terminal.

Now, consider the following command.

sleep 10 &

The above command displays the process id (PID), and sleep 10 executes asynchronously in the background. The execution control returns to the command terminal without waiting for sleep to end.

Now, we can run any other command on the same terminal concurrent to the background sleep command.

We can move a background process to the foreground using the following command.

fg

the nohup Command

The nohup command runs any other command or process. It stands for “no hang-up”, which prevents the associated process from getting the SIGHUP signal.

If you want to execute a command even after the terminal is closed, you can use nohup CommandName.

However, if we want to run a command in the background with the execution control immediately returned to the terminal, we have to use the following command.

nohup sleep 10 &

The above command runs the sleep 10 command in the background and returns control immediately so that we can run any other command on the same terminal.

We can check the commands running in the background using the pgrep command as follows:

pgrep -a [Command]

The pgrep command searches for the Command and shows the process id (PID) along with the executing command details.

For example, pgrep -a sleep will display the relevant background process as:

PID sleep 10

Here, PID represents the process id assigned to the sleep command.

Difference Between Control Operator & and nohup Command

The following are the few differences between running a command or process in the background using & and nohup.

  1. nohup can catch the hang-up signal (SIGHUP), while & can’t. The SIGHUP signal is used to send a signal to the processes when the terminal is closed from which the process is started.

  2. Typically, a process or command runs in the background using & until the shell exists from which this command or process is started. Once the shell is terminated, all the associated commands or processes running in the background using & are also terminated.

    When a terminal exits, a hang-up signal using SIGHUP (kill SIGHUP <pid>) terminates all sub-commands or child processes of that terminal. However, this can be prevented using nohup.

    The nohup command catches the SIGHUP signal and does not let it reach the actual command. Therefore, restricting the command from terminating as the Bash terminal exits.

  3. Another difference between & and nohup is about redirection of stdout/stderr.

    The & operator does not redirect the stdout/stderr automatically and displays the output of the commands directly to the terminal. However, nohup redirects the stdout/stderr in a file nohup.out located at $HOME.