How to use nohup Command in Linux

The signal in Linux or other UNIX like operating systems is a notification or message sent to a process to notify it that some event has occurred. Most of the commands executed by a user are run by his/her shell. The parent process of these commands or scripts is his shell, whose process ID can be checked with $$.

The SIGHUP signal is the hangup signal which is sent to a process when its controlling terminal or the controlling process is dead. The nohup command is the command that runs a process ignoring SIGHUP signal.

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

Exit status

Let's check exist status for nohup

125 if `nohup' itself fails, and `POSIXLY_CORRECT' is not set.
126 if COMMAND is found but cannot be invoked.
127 if COMMAND cannot be found.
the exit status of COMMAND otherwise.

Bobbin Zachariah 11:01 pm

About Bobbin Zachariah

Founder of LinOxide, passionate lover of Linux and technology writer. Started his career in Linux / Opensource from 2000. Love traveling, blogging and listening music. Reach Bobbin Zachariah about me page and google plus page.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.


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

All comments are subject to moderation.