nohup command, short for no hang-up is used to run a process in the background. A process that has been ‘nohuped’ will continue to run even if the terminal from which it was run is closed. In effect,
nohup disconnects the process from the terminal.
nohup Command Basic Usage
Before going into the usage of
nohup, it is important to understand that it can only be used when the process isn’t already running and is yet to start. (There are other methods to
nohup a process which is already running using the
Now that we know where nohup can and cannot be used, it is pretty easy to
nohup a process. Just prepend the command with nohup:
Running nohup does the following things:
- It ignores the SIGHUP signal for the program. (This behavior, however, can be changed inside the program. More on this exception later in the article.)
- It closes the
stdin(the program will not be able to read any input, but will receive an error code or
EOFif it attempts to read).
- It redirects
stderrto the file
nohup.out, so the program won’t fail when writing to standard output if the terminal fails. This way, whatever the program writes will not be lost.
Let’s test the
nohup command now. We will ping
nohup ping www.linuxfordevices.com
This, however, will hog our terminal if the command is a long-running one(
ping doesn’t end until interrupted and can be considered a long-running command) . This is because the ping command is in the foreground. The ping would still continue even if you closed the terminal now.
If you want to continue using your terminal for other tasks after ‘nohuping’ a command, you need to put this command in the background.
Nohup without hogging the terminal
To put the program in the background without hogging up the terminal, append an ampersand ‘
&‘ to the end of the command. Let’s do that for the previous example:
nohup <command> &
In our case, type
nohup ping www.linuxfordevices.com &
Now you can safely close the terminal and the process will continue to run in the background. To be assured, let’s check the process in a different terminal using the ps command.
ps aux | grep ping
As can be seen in the screenshot, there is a ‘
?‘ in the output for the ping. This ‘
?‘ indicates that the process – in our case ping – is not attached to any terminal. Whereas, just below the `
/dev/pts0 is the terminal for the
grep command we just ran.
Kill a nohuped process
To kill any process, let alone a nohuped one, you need to know the Process ID (PID) of the process. It can be easily obtained using the ps command. The second column of the output gives the PID.
ps aux | grep ping
Then, to kill a process using the kill command type
Nohup command examples
Let’s now see the practical uses of the nohup command.
1. Run a GUI application through terminal
You might have tried to run Chrome or VLC through the terminal. But the problem, in this case, is that these applications are attached to the terminal, and on closing the terminal a SIGHUP signal is sent to these applications. The applications generally exit on receiving this signal.
This is where
nohup is useful. To run VLC in the background so that it does not exit on closing the terminal, type
nohup vlc &
Now, without any file redirection
nohup will write all output/errors to a
nohup.out file in the home directory. This may be desired in certain circumstances but it’s better, in this case, to just ignore the output/errors VLC produces. Our final one-liner would be now
nohup vlc >/dev/null 2>&1 &
Redirect standard output to
/dev/null. Redirect errors (
2 ) to wherever standard output goes (
Chrome – An exception to Nohup
Some applications like Chrome, have a known issue that they will exit when the terminal is closed even when run with the
nohup command. To read more about this visit this StackExchange answer.
This happens because an application may change the behavior when it receives the SIGHUP signal, bypassing
The solution is to use the
disown command as follows
/usr/bin/google-chrome & disown $!
2. Run servers in the background
Running severs in the background can be very useful for developers. Since mostly these servers are started using commands like
npm start #or python manage.py runserver
These servers, like any other commands, can be used with
nohup to allow them to run in the background while the same terminal can be used for other tasks.
To run, for example, a Python Django server in the background, type
nohup python manage.py runserver >~/server_logs 2>&1 &
The output and errors are redirected to
Nohup is a very useful command to run processes in the background. It provides a quick and easy way to create daemons and background jobs. To know more about nohup check out its man page. I hope you learned something new about the
nohup command through this article. Let us know in the comments which use of nohup you didn’t already know.