The nohup command in Linux (with examples) – Run processes in the background

Nohup Command

The 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 disown command.)

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:

nohup <command>

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 EOF if it attempts to read).
  • It redirects stdout and stderr to 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 www.linuxfordevices.com.

nohup ping www.linuxfordevices.com
nohup command with ping
nohup command

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 &
Image 4
Running ping in the background

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
Image 1
Watching the ping from a different terminal

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

kill <PID>
Image 3
Kill the process

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 &
Nohup Vlc
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 &

Explanation:

Redirect standard output to /dev/null. Redirect errors ( stderr or 2 ) to wherever standard output goes ( stdout or 1 ).

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 nohup.

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 &
Server Using Nohup
Server Using Nohup

The output and errors are redirected to ~/server_logs.


Conclusion

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.