Nohup
Template:Short description Template:Lowercase {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other nohup is a POSIX command which means "no hang up". Its purpose is to execute a command such that it ignores the HUP (hangup) signal and therefore does not stop when the user logs out.
Output that would normally go to the terminal goes to a file called Template:Mono, if it has not already been redirected.
UseEdit
The first of the commands below starts the program abcd
in the background in such a way that the subsequent logout does not stop it.
<syntaxhighlight lang="console">
$ nohup abcd &
$ exit
</syntaxhighlight>
Note that these methods prevent the process from being sent a 'stop' signal on logout, but if input/output is being received for these standard I/O files (stdin, stdout, or stderr), they will still hang the terminal.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> See Overcoming hanging, below.
nohup is often used in combination with the nice command to run processes on a lower priority. <syntaxhighlight lang="console"> $ nohup nice abcd & </syntaxhighlight>
ImplementationsEdit
Some shells (e.g. bash) provide a shell builtin that may be used to prevent SIGHUP being sent or propagated to existing jobs, even if they were not started with nohup. In bash, this can be obtained by using disown -h job
; using the same builtin without arguments removes the job from the job table, which also implies that the job will not receive the signal. Before using disown
on an active job, it should be stopped by Ctrl-Z
, and continued in the background by the bg
command.<ref>Bash Reference Manual Template:Webarchive. Gnu.org. Retrieved on 2015-04-13.</ref> Another relevant bash option is shopt huponexit
, which automatically sends the HUP signal to jobs when the shell is exiting normally.<ref>Bash Reference Manual Template:Webarchive. Gnu.org. Retrieved on 2015-04-13.</ref>
The AIX and Solaris versions of nohup have a -p
option that modifies a running process to ignore future SIGHUP signals. Unlike the above-described disown
builtin of bash, nohup -p
accepts process IDs.<ref>IBM Knowledge Center Template:Webarchive. 01.ibm.com (2015-03-26). Retrieved on 2015-04-13.</ref>
The Template:Mono command has also been ported to the IBM i operating system.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Overcoming hangingEdit
Note that nohupping backgrounded jobs is typically used to avoid terminating them when logging off from a remote SSH session. A different issue that often arises in this situation is that ssh is refusing to log off ("hangs"), since it refuses to lose any data from/to the background job(s).<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> This problem can also be overcome by redirecting all three I/O streams: <syntaxhighlight lang="console"> $ nohup ./myprogram > foo.out 2> foo.err < /dev/null & </syntaxhighlight>
Also note that a closing SSH session does not always send a HUP signal to dependent processes, such as when a pseudo-terminal has not been allocated.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
AlternativesEdit
- A terminal multiplexer can run a command in a separate session, detached from the current terminal, which means that if the current session ends, the detached session and its associated processes keeps running. One can then reattach to the session later on.
- For example, the following invocation of screen will run somescript.sh in the background of a detached session:
<syntaxhighlight lang="console"> $ screen -A -m -d -S somename ./somescript.sh & </syntaxhighlight>
- The disown command is used to remove jobs from the job table, or to mark jobs so that a SIGHUP signal is not sent on session termination.