Example How To Use Linux nohup Command

November 23, 2012 | By
| Reply More

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.

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

Options to nohup command

There are only two common options available to this command:

--help: display the help and exit

$ nohup --help
Usage: nohup COMMAND [ARG]...
or: nohup OPTION
Run COMMAND, ignoring hangup signals.

--help display this help and exit
--version output version information and exit

If standard input is a terminal, redirect it from /dev/null.
If standard output is a terminal, append output to `nohup.out' if possible,
`$HOME/nohup.out' otherwise.
If standard error is a terminal, redirect it to standard output.
To save output to FILE, use `nohup COMMAND > FILE'.

NOTE: your shell may have its own version of nohup, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.

Report nohup bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'nohup invocation'

--version: output version information and exit

$ nohup --version
nohup (GNU coreutils) 8.5
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.

Exit status

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.

Filed Under : HOWTOS, LINUX COMMANDS, LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.