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 command syntax:
You can use the nohup command by two ways.
- nohup command [args…]
- 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.
$ 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.
$ 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’
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.
$ 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.
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.
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.
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.
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.