How to Use nohup Command with Examples

When you execute a Linux command and log out after some time from the session if that command has not finished executing,  it's process will get killed. Solution to this problem is a tool called nohup.

Nohup stands for 'no hang up', and as the name indicates prevents the command from being aborted automatically when you log out or exit the shell.
There are all sorts of programs that require many hours to complete. We don’t need to wait for the command to complete the whole task. We can keep programs running in the background by using nohup command and check the output later.

nohup command

The syntax of the nohup command is:

nohup COMMAND [ARG]...

As the controlling terminal is closed, so there are some issues that need to be considered. First of all, the process continues to run even if the controlling terminal is closed. The second issue is the output of the command. By default the output of a command is displayed on the standard output device which is the terminal that initiated the process. But as the terminal may be closed during the life of the child process, so terminal cannot be the output device anymore. According to the info page of nohup command,

If standard output is a terminal, the command's standard output is appended to the file `nohup.out'; if that cannot be written to, it is appended to the file `$HOME/nohup.out'; and if that cannot be written to, the command is not run. Any `nohup.out' or `$HOME/nohup.out' file created by `nohup' is made readable and writable only to the user, regardless of the current umask settings.

Similarly for standard error,

If standard error is a terminal, it is normally redirected to the same file descriptor as the (possibly-redirected) standard output. However, if standard output is closed, standard error terminal output is instead appended to the file `nohup.out' or `$HOME/nohup.out' as above.

It means that by default both standard output and standard error are redirected to 'nohup.out' file. But if you wish to redirect the output to some other file, you can always do so by using redirect operator. For example, to catch the output of make,

nohup make > make.log

For standard input,

If standard input is a terminal, it is redirected from `/dev/null' so that terminal sessions do not mistakenly consider the terminal to be used by the command. This is a GNU extension; programs intended to be portable to non-GNU hosts should use `nohup COMMAND [ARG]...

The nohup command does not automatically put the command in background. This must be done explicitly with &.

nohup make > make.log &

Also, this command does not change the niceness (i.e. the priority) of a command. For this, nice is used. e.g. 'nohup nice COMMAND'.

Nohup Usage Example

We can use nohup in two ways. First is running the command with parameters without receiving any other input while working. All output, including any error messages, will be written to the file nohup.out in the working directory, or in your home directory. If the command is still running when you log out or close the terminal, it will not terminate. Let's try an example with memtester application.

$ nohup sudo memtester 2048 5
nohup: ignoring input and appending output to 'nohup.out'
Killed

$ cat nohup.out 
memtester version 4.3.0 (64-bit)
Copyright (C) 2001-2012 Charles Cazabon.
Licensed under the GNU General Public License version 2 (only).

pagesize is 4096
pagesizemask is 0xfffffffffffff000
want 2048MB (2147483648 bytes)
got  1876MB (1968074752 bytes), trying mlock ...

You could also redirect the output to some other file instead of nohup.out using > symbol.

$ nohup sudo memtester 2048 5 > result.txt

Using nohup command with ‘&’

To start a process and put it in the background, you basically execute the command followed by "&" symbol. The "&" symbol at the end of the command instructs bash to run nohup yourcommand in the background. It can be brought back to the foreground with the fg bash builtin command.

$ nohup bash sleep1.sh &
[1] 653
$ nohup: ignoring input and appending output to 'nohup.out'
fg
-bash: fg: job has terminated
[1]+  Exit 127                nohup bash sleep1.sh

Run multiple commands in the background

You can run multiple commands in the background by using nohup command. In the following example, mkdir, touch and ls commands are executed in the background by using nohup and bash commands.

$ nohup bash -c 'mkdir TestDir && touch test2.file && ls'> output.txt
nohup: ignoring input and redirecting stderr to stdout

$ cat output.txt 
TestDir
nohup.out
output.txt
test2.file
testfile.txt

Starting and ending process in background

If you run the ping command using nohup, the process will not terminate when you close the terminal.

$ nohup ping -i 15 linoxide.com &
[1] 806
$ nohup: ignoring input and appending output to 'nohup.out'

You can close the terminal and reopen it. Now run pgrep command with -a option.

$ pgrep -a ping
816 ping -i 15 linoxide.com

You will get the list of processes with the process id which is running. As you can see, the ping command we left running is not terminated.
To terminate any background process, you have to run kill command. As a parameter, we must use the particular process id which is running. In our case, the process id is 816. Run kill 816 to terminate the process.

$ pgrep -a ping
816 ping -i 15 linoxide.com
$ kill 816

Conclusion

Nohup is very helpful when you have to execute a shell script. Hopefully, you are now familiar with this tool which can be useful in many cases. If you have any questions or comments, please post them below.

Bobbin Zachariah 11:01 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.