What is the Difference Between nohup, disown, and &

In Linux, the nohup, disown, and & commands are used to run a command or process in the background so that it continues to run even after the user logs out of the terminal. It is beneficial when the processes take too much time to complete.

This guide will describe the difference between nohup, disown, and & in Linux.

  • nohup Command in Linux
  • Example 
  • disown in Linux
  • Example 
  • Ampersand (&) in Linux
  • Example
  • Difference between &, disown, and nohup

nohup in Linux

The “nohup” is the built-in command of Linux examined for running the tasks in the background without any interruption even when the terminal session is closed. By default, it creates the “nohup.out” file and writes the output in this file. Below is the syntax for using the nohup command.

Syntax

$ nohup [Command]
  • The “nohup” command to send the process to the background
  • Enter the [Command]

Example

The nmap command is used to scan ports for the IP address “192.168.119.129”. And the nohup command is used to run in the background and write the output in the “nohup.out” file as shown:

$ nohup sudo nmap -sS --top-ports=15 192.168.119.129/24

The output is appended to the “nohup.out” file.

Verify the content of the “nohup.out” file using the cat command:

$ cat nohup.out

The output for scanning the port through the nmap command has been written in the file.

disown in Linux

The “disown” is the built-in command in Linux utilized for deleting the background jobs. It also prevents the background processes/jobs from terminating when the terminal closes. 

For using the disown command the following syntax is considered.

Syntax 

$ disown [options] [Job_ID1, Job_ID2.....Job_IDn]
  • Type the “disown” command to delete the background job.
  • The “options” along with the disown command. 
  • Enter job IDs to delete the particular jobs.

Example

To delete the background job using disown, first list all jobs and delete the background jobs based upon the requirement. In this way, all the running background jobs will be deleted:

$ jobs -l
$ disown -r

As there was only one running job available, the background job having ID 11584 has been deleted.

The user can check that job is deleted by running the “job -l” command:

$ jobs -l

The job was deleted and there is no job to display.

Ampersand(&) in Linux

The ampersand (&) is the operator in Linux that is considered for sending the execution of the command to the background. It will allocate the job id to a particular process/ task and run it in the background. For using the ampersand (&), the following syntax is utilized:

Syntax

$ [command] &
  • Type any [command] to perform the tasks.
  • At the end, put the ampersand (&) operator.

Example

Let’s say the user wants to add 2 tasks to the background. One is opening the gnome editor and another is downloading the TeamViewer software. To do so, put the “&” operator at the end of the command as shown:

$ gedit &
$ wget https://download.teamviewer.com/download/linux/teamviewer_amd64.deb &

The gnome editor is running in the background with job ID 11584 and TeamViewer file is downloading in the background with job ID 1160.

To list all jobs running in the background, the user can use the “jobs” command with option “l”:

$ jobs -l

Currently, two jobs are running in the background: “geddit” for gnome editor and “wget” for downloading files.

Note: When the user exit from the terminal, all the background jobs will be deleted 

Difference between nohup, disown, and Ampersand(&)

The difference between nohup, disown, and ampersand(&) is described in the below table:

nohupdisownAmpersand (&)
The command is utilized for running the process in the background.The command for deleting the background jobs. The ampersand(&) operator is utilized for running the process in the background.
Keep the job running even if the terminal is closed.It also prevents the background job from termination if using the “h” flag.All the background jobs will be terminated once the terminal is closed.
By default. It redirects the output of the command in the “nohup.out” fileIt Doesn’t write any data into a file.It Doesn’t write any data into a file.

Conclusion

In Linux, the nohup command runs the task in the background without any interruption, and the disown command deletes as well as secures the background tasks. While the ampersand(&) operator runs the job in the background. The syntax for using the no hup, disown command and ampersand(&) are “$ nohup [Command]”, “disown [Options] [Job_ID1, Job_ID2…..Job_IDn]” and “$ [command] &”. 
This write-up has illuminated the difference between the ampersand(&) operator, “disown” and “nohup” commands in Linux.