Tips SSH

Nohup Command in Linux Enables You to Run Commands Even After Logging Out

This article will show you how you can start a process and keep it running even after you have logged out using the nohup command.

Eric Simard
Eric Simard

Nohup stands for ‘no hang up’. It’s an extremely useful command to make your process running even after you log out.

One of the most common use can be found in running time-taking commands over SSH connection. If you think that your SSH session may drop, you can use the command with nohup in this fashion:

nohup your_command &

What is nohup command?

Nohup can be seen as a wrapper. You prefix your command with nohup like this:

nohup COMMAND ARGS ....

Nohup‘s purpose is to intercept and prevent SIGHUP signals from reaching COMMAND.

When you prefix your command with nohup, the input will be redirected from an unreadable file ( nohup COMMAND < /dev/null ). In simple word, don’t call a command with nohup if you need to send input to that command. When you nohup a command, it needs to be able to run unattended.

Nohup will first attempt to redirect the output into ./nohup.out or will send it to ~/nohup.out if need be. You can also decide where to redirect the output like this:

nohup COMMANDS >/path/to/output/file

There is not much to say about nohup, it does not have a long list of options. There is only –help and –version.

Example of using nohup command

Executing a script with nohup is as simple as this.

nohup ./nhp.sh
nohup command

Note that nohup did not fork the process to the background. Nohup is only there to intercept SIGHUP signals. Once the command is running you can proceed and close your terminal. It will warn you that a process is running. Confirm you want to kill it.

nohup command while logging out of the session

When you closed your terminal, all child processes received a SIGHUP signal. Nohup prevented that signal from reaching my command. The following picture shows process information about my command while the terminal was still running.

You see the PPID? That is the parent process ID. When you requested to close the terminal, all children of PPID received a SIGHUP signal. Nohup prevented that signal from reaching our script. The parent process terminated and our script got orphaned. When a process gets orphaned, it automatically gets assign 1 (systemd or init) as a parent.

As you can see it is really not hard to demonstrate the use of nohup. Very simple command. You can fork the process in the background like this:

nohup ./nhp.sh &

You can also decide where the output is redirected to like so:

nohup ./nhp.sh >/path/to/file &

Note that if you shutdown or reboot Linux system, it will kill the command. When you request a shutdown or reboot, all processes receives a SIGTERM and then a SIGKILL. Those signals are not intercepted by nohup.

Nohup through SSH connection

One use that often comes to mind right away when you learn nohup is to be able to start a process on a remote computer through SSH. Logically, running a command with nohup would allow you to logoff from your SSH connection and the process would still run.

nohup COMMAND &

One common problem is that sometimes SSH will “hang” while logging off. This is because SSH will often refuse to lose any data stream to and from any background processes. It will then “hang” until the process is terminated.

This issue can often be dealt with by redirecting all 3 data stream.

nohup COMMAND >./nohup.out 2>./nohup.err &

I hope this quick article helped you to better use SSH sessions. If you have questions or suggestions, please leave a comment below.



Join the conversation.