Nohup command in Linux with Examples

The nohup command in Linux is used to tell a process to ignore any SIGHUP (hangup) signals that it receives. The SIGHUP signal is sent to a background job whenever the current terminal is closed. Thus, we can have a job in the background, close the terminal, and still see it running in the background.

In this tutorial, you will learn how to use the nohup command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the nohup command on Linux
nohup command in Linux with examples
nohup command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software nohup
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options




Every process which starts from a terminal is tied to the shell as a child process from which it was executed. In the situation where a parent program gets terminated, the child process will be terminated also.

This is not particularity desired behavior when a user needs to run a process remotely and let the process run after logout. For this reason, the nohup command exists. The nohup command in Linux ensures that the process is not tied to any particular shell, so the user can kill a shell process by logging out and the process executed with nohup will be kept alive.

Check out the examples below to master the nohup Linux command in short time.

nohup command in Linux Examples

  1. To use the nohup command, simply run a command as you normally would, but put the nohup command in front of it. Here is an example where we put the sleep command into the background.
    $ nohup sleep 10000 &
    [1] 1908
    nohup: ignoring input and appending output to 'nohup.out'
    

    We have closed the terminal and reopened it, then we ran the following command, which shows that the sleep command is still running, thanks to nohup.

    $ ps aux | grep sleep
    linuxco+    1908  0.0  0.0   8084   524 ?        S    13:05   0:00 sleep 10000
    

    Keep in mind that you can bring the background job to the foreground with the fg command.

    $ fg
    
  2. By default, output is redirected to the nohup.out text file. If you would like to redirect output to a different file, just specify the name of the file in your command.
    $ nohup some_script.sh > output.txt
    
  3. To run multiple commands with nohup, use the bash command then the various commands that you want to run.
    $ nohup bash -c 'who && uptime'
    
NOTE
You can always use the man command to read more about the nohup command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.


Closing Thoughts

In this tutorial, we learned all about the nohup command on Linux. The nohup command is a very simple command that is used in order to keep a command running even when the terminal or Bash shell is closed by ignoring the hangup signal sent to the command. Ordinarily, this is also coupled with an ampersand & in order to continue running some command or script in the background.