{{Short description|POSIX (computer) command}} {{Lowercase title}} {{Infobox software | name = nohup | logo = | screenshot = | screenshot size = | caption = | author = | developer = Various open-source and commercial developers | released = | latest release version = | latest release date = | operating system = Unix, Unix-like, IBM i | platform = Cross-platform | genre = Command | license = | website = }} '''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 {{mono|nohup.out}}, if it has not already been redirected.
== Use == The first of the commands below starts the program <code>abcd</code> 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>{{cite web |url=http://www.zsh.org/mla/users/2005/msg00152.html |title=Re: nohup/disown and logout |publisher=Zsh.org |date=2005-02-07 |accessdate=2009-06-10 |url-status=live |archiveurl=https://web.archive.org/web/20090518121423/http://www.zsh.org/mla/users/2005/msg00152.html |archivedate=2009-05-18 }}</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 & $ exit </syntaxhighlight>
==Implementations== 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 <code>disown -h job</code>; 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 <code>disown</code> on an active job, it should be stopped by <code>Ctrl-Z</code>, and continued in the background by the <code>bg</code> command.<ref>[https://www.gnu.org/software/bash/manual/bashref.html#Job-Control-Builtins Bash Reference Manual] {{webarchive|url=https://web.archive.org/web/20101203065719/https://www.gnu.org/software/bash/manual/bashref.html |date=2010-12-03 }}. Gnu.org. Retrieved on 2015-04-13.</ref> Another relevant bash option is <code>shopt huponexit</code>, which automatically sends the HUP signal to jobs when the shell is exiting normally.<ref>[https://www.gnu.org/software/bash/manual/bashref.html#The-Shopt-Builtin Bash Reference Manual] {{webarchive|url=https://web.archive.org/web/20101203065719/https://www.gnu.org/software/bash/manual/bashref.html |date=2010-12-03 }}. Gnu.org. Retrieved on 2015-04-13.</ref>
The AIX and Solaris versions of nohup have a <code>-p</code> option that modifies a running process to ignore future SIGHUP signals. Unlike the above-described <code>disown</code> builtin of bash, <code>nohup -p</code> accepts process IDs.<ref>[https://www-01.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.cmds4/nohup.htm IBM Knowledge Center] {{webarchive|url=https://web.archive.org/web/20141015050913/https://www-01.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.cmds4/nohup.htm |date=2014-10-15 }}. 01.ibm.com (2015-03-26). Retrieved on 2015-04-13.</ref>
The {{Mono|nohup}} command has also been ported to the IBM i operating system.<ref>{{cite web |title=IBM System i Version 7.2 Programming Qshell |language=en |author=IBM |website=IBM |author-link=IBM |url=https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_74/rzahz/rzahzpdf.pdf?view=kc |access-date=2020-09-05 }}</ref>
==Overcoming hanging== 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>{{cite web |url=http://www.snailbook.com/faq/background-jobs.auto.html |title=SSH Frequently Asked Questions |publisher=Snailbook.com |accessdate=2009-06-10 |url-status=live |archiveurl=https://web.archive.org/web/20090122224848/http://www.snailbook.com/faq/background-jobs.auto.html |archivedate=2009-01-22 }}</ref><ref>{{cite web |url=http://www.openssh.com/faq.html#3.10 |title=OpenSSH FAQ |publisher=Openssh.com |date=2005-09-20 |accessdate=2009-06-10 |url-status=dead |archiveurl=https://web.archive.org/web/20090710072355/http://www.openssh.com/faq.html#3.10 |archivedate=2009-07-10 }}</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>{{cite web|url=https://bugzilla.mindrot.org/show_bug.cgi?id=396 |title=Bug 396 – sshd orphans processes when no pty allocated |publisher=Bugzilla.mindrot.org |date= |accessdate=2009-06-10}}</ref>
==Alternatives== * 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 shell builtin is used in some shells to remove jobs from the job table, or to mark jobs so that a SIGHUP signal is not sent on session termination.
==References== {{Reflist}}
==External links== *[https://web.archive.org/web/20061028142027/http://bama.ua.edu/cgi-bin/man-cgi?nohup+1 nohup from Solaris's man pages]
{{Unix commands}} {{Core Utilities commands}}
Category:Unix SUS2008 utilities Category:IBM i Qshell commands