Linux Commands

Nohup Command in Linux

Bash nohup command tutorial

The meaning of nohup is ‘no hangup‘.  Normally, when we log out from the system then all the running programs or processes are hangup or terminated.  If you want to run any program after log out or exit from Linux operating system then you have to use nohup command. There are many programs that require many hours to complete. We don’t need to log in for long times to complete the task of the command. We can keep these type of programs running in the background by using nohup command and check the output later. Some example of using nohup command are memory check, server restart, synchronization etc. How you can use a nohup command on Ubuntu to run a program in the background is shown in this tutorial.

You can check the version of nohup command by using the following command.

$ nohup   --version

nohup command syntax:

You can use the nohup command by two ways.

  1. nohup command [args…]
  2. nohup option

Using nohup with commands

Example-1: Using nohup command without ‘&’

When you run nohup command without ‘&’ then it returns to shell command prompt immediately after running that particular command in the background. In the following example, nohup run bash command without ‘&’ to execute sleep1.sh file in the background.  The output of the nohup command will write in nohup.out the file if any redirecting filename is not mentioned in nohup command. For the following command, you can check the output of sleep1.sh by checking the output of nohup.out file.

$ nohup bash sleep1.sh
$ cat nohup.out

You can execute the command in the following way to redirect the output to the output.txt file. Check the output of output.txt.

$ nohup bash sleep2.sh > output.txt
$ cat output.txt

Example-2: Using nohup command with ‘&’

 When nohup command use with ‘&’ then it doesn’t return to shell command prompt after running the command in the background. But if you want you can return to shell command prompt by typing ‘fg’

$ nohup bash sleep1.sh &
$ fg

Example-3: Using nohup command to run multiple commands in the background

You can run multiple commands in the background by using nohup command. In the following command, mkdir and ls command are executed in the background by using nohup and bash commands. You can get the output of the commands by checking output.txt file.

$ nohup bash -c 'mkdir myDir && ls'> output.txt
$ cat output.txt

Example-4: Start any process in the background by using nohup

When any process starts and the user closes the terminal before completing the task of the running process then the process stops normally. If the run the process with nohup then it will able to run the process in the background without any issue. For example, if you run the ping command normally then it will terminate the process when you close the terminal.

$ ping -i 10 google.com

You can check the list of all running command by using pgrep command. Close the terminal. Re-open the terminal and run pgrep command with -a option. No list of running command will display because all running commands are terminated when the terminal was closed.

$ pgrep -a ping

Run ping command with nohup command. Re-open the terminal and run pgrep command again. You will get the list of the process with process id which is running.

$ nohup ping -i 15 google.com &

$ pgrep -a ping

You can stop any background process by running kill command. Just run kill command with particular process id which is running. Here, the process id of the running process is 7015. Run kill command in 7015 to terminate the process.

$ kill 7015

The uses of nohup command are explained by using very simple examples in this tutorial. Hope you will get a clear idea of the function of nohup command and able to apply this command for various purposes.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.