- nohup Command in Linux with Examples
- Working with nohup command
- How to Use Linux nohup Command
- nohup Syntax
- nohup Options
- nohup Examples
- 1. Running a Process with nohup
- 2. Running a Process in the Background with nohup
- 3. Running Multiple Processes in the Background with nohup
- 4. Redirecting Output to a Different File
- Bash nohup command tutorial
- nohup command syntax:
- Example-1: Using nohup command without ‘&’
- Example-2: Using nohup command with ‘&’
- Example-3: Using nohup command to run multiple commands in the background
- Example-4: Start any process in the background by using nohup
- About the author
- Fahmida Yesmin
nohup Command in Linux with Examples
Every command in Linux starts a process at the time of its execution, which automatically gets terminated upon exiting the terminal. Suppose, you are executing programs over SSH and if the connection drops, the session will be terminated, all the executed processes will stop, and you may face a huge accidental crisis. In such cases, running commands in the background can be very helpful to the user and this is where nohup command comes into the picture. nohup (No Hang Up) is a command in Linux systems that runs the process even after logging out from the shell/terminal.
Usually, every process in Linux systems is sent a SIGHUP (Signal Hang UP) which is responsible for terminating the process after closing/exiting the terminal. Nohup command prevents the process from receiving this signal upon closing or exiting the terminal/shell. Once a job is started or executed using the nohup command, stdin will not be available to the user and nohup.out file is used as the default file for stdout and stderr. If the output of the nohup command is redirected to some other file, nohup.out file is not generated.
nohup command [command-argument . ]
Working with nohup command
1. Checking the version of Nohup:
The version of nohup command can be checked by using the following command.
2. To run a command in the foreground:
Runs the command in the foreground and redirects the command output to the nohup.out file if any redirecting filename is not mentioned.
To redirect the output to the output.txt file:
$ nohup bash geekfile.sh > output.txt
3. To run a command in the background (with ‘&’):
To run the command in the background, the ‘&’ symbol is appended at the end of the command. After executing, it doesn’t return to the shell command prompt after running the command in the background. It can be brought back to the foreground with the fg command.
$ nohup bash geekfile.sh & fg
Note: The number within square brackets represents the job id and the number next to it is the process id.
4. To run multiple commands in the background:
nohup command can be used to run multiple commands in the background.
Here, the output will be by default stored in nohup.out. To redirect it, type:
$ nohup bash -c 'commands' > filename.txt
$ nohup bash -c 'cal && ls' > output.txt
How to Use Linux nohup Command
SIGHUP (Signal Hang UP) is a signal that terminates a Linux process when its controlling terminal is closed. If you accidentally close a terminal or lose connection with the server, all processes running at the time are automatically terminated.
Using the nohup command is one way of blocking the SIGHUP signal and allowing processes to complete even after logging out from the terminal/shell.
In this article, you will learn how to use the nohup command while running processes.
nohup Syntax
The syntax for using the nohup command is:
nohup Options
The nohup command features two basic command options, the —help and the —version options.
To display the help message, run the command:
To display version information, type:
Note: Although nohup may appear similar to daemons, there is a significant difference. Daemons are reserved for processes that continuously run in the background, while nohup is used for processes that take a long time but don’t continue running once done.
nohup Examples
There are a number of ways to use the nohup command, including running the required process in the background, running multiple processes simultaneously, or redirecting the output to a different file.
The examples below explain common use cases for the nohup command.
1. Running a Process with nohup
To run a command using nohup without any arguments, simply follow the syntax:
The shell ignores the output and appends it to the nohup.out file.
For instance, running the example.sh bash script (which is a simple Hello World script) should prompt the Hello World message in the nohup.out file:
Verify the contents of the file with:
2. Running a Process in the Background with nohup
Running a Linux process in the background frees up the terminal you are working in. To run a Linux process in the background with the nohup command, add the & symbol at the end of the command:
For example, to run the example.sh bash script in the background, use the command:
The output displays the shell job ID and process ID — [1] 7366 in the example below.
To bring the command in the foreground, type:
The output indicates whether the process is in progress or complete.
Note: For a list of all the important Linux commands in one place, check out our Linux Commands Cheat Sheet.
3. Running Multiple Processes in the Background with nohup
nohup bach -c '[command1] && [command2]'
Replace [command1] and [command2] with the commands of your choice. Add more commands if necessary, making sure to separate them with && .
For instance, to show the date/time and the calendar of the current month, run:
As the output is directed to the nohup.out file, use the cat command to list the contents of the file and verify the command above:
The output shows the date and calendar prompted with the command above.
4. Redirecting Output to a Different File
As mentioned in the section above, nohup logs all output messages and errors into the nohub.out file.
Redirect these messages by specifying a custom location within the command:
nohup [command] > /path/to/output/file.txt
In the example below, the output for the command nohup bash -c ‘date && cal’ is redirected to the output.txt file. Check the output with the command:
After reading this article, you should know how to use the nohup command to run processes in the background and redirect their output.
As an alternative to using the nohup command, consider checking out Tmux. Tmux was built to support multitasking in a terminal window. Learn more in our comprehensive Tmux Tutorial.
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.
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.
You can execute the command in the following way to redirect the output to the output.txt file. Check the output of 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.
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.
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.