Jump to content

nohup

From Wikipedia, the free encyclopedia
nohup
Developer(s)Variousopen-sourceandcommercialdevelopers
Operating systemUnix,Unix-like,IBM i
PlatformCross-platform
TypeCommand

nohupis aPOSIXcommand which means "no hang up". Its purpose is to execute a command such that it ignores theHUP(hangup) signal and therefore does not stop when the user logs out.

Output that would normally go to the terminal goes to a file callednohup.out,if it has not already been redirected.

Use

[edit]

The first of the commands below starts the programabcdin the background in such a way that the subsequent logout does not stop it.

$nohupabcd&
$exit

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.[1]SeeOvercoming hanging,below.

nohup is often used in combination with thenicecommand to run processes on a lower priority.

$nohupniceabcd&

Implementations

[edit]

Some shells (e.g.bash) provide ashell builtinthat may be used to preventSIGHUPbeing sent or propagated to existing jobs, even if they were not started with nohup. In bash, this can be obtained by usingdisown-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 usingdisownon an active job, it should be stopped byCtrl-Z,and continued in the background by thebgcommand.[2]Another relevant bash option isshopthuponexit,which automatically sends the HUP signal to jobs when the shell is exiting normally.[3]

TheAIXandSolarisversions of nohup have a-poption that modifies a running process to ignore future SIGHUP signals. Unlike the above-describeddisownbuiltin of bash,nohup -paccepts process IDs.[4]

Thenohupcommand has also been ported to theIBM ioperating system.[5]

Overcoming hanging

[edit]

Note that nohupping backgrounded jobs is typically used to avoid terminating them when logging off from a remoteSSHsession.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).[6][7]This problem can also be overcome byredirectingall three I/O streams:

$nohup./myprogram>foo.out2>foo.err</dev/null&

Also note that a closing SSH session does not always send a HUP signal to dependent processes, such as when apseudo-terminalhas not been allocated.[8]

Alternatives

[edit]
  • Aterminal multiplexercan 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 ofscreenwill run somescript.sh in the background of a detached session:
$screen-A-m-d-Ssomename./somescript.sh&
  • Thedisowncommand is used to remove jobs from the job table, or to mark jobs so that a SIGHUP signal is not sent on session termination.

References

[edit]
  1. ^"Re: nohup/disown and logout".Zsh.org. 2005-02-07.Archivedfrom the original on 2009-05-18.Retrieved2009-06-10.
  2. ^Bash Reference ManualArchived2010-12-03 at theWayback Machine.Gnu.org. Retrieved on 2015-04-13.
  3. ^Bash Reference ManualArchived2010-12-03 at theWayback Machine.Gnu.org. Retrieved on 2015-04-13.
  4. ^IBM Knowledge CenterArchived2014-10-15 at theWayback Machine.01.ibm (2015-03-26). Retrieved on 2015-04-13.
  5. ^IBM."IBM System i Version 7.2 Programming Qshell"(PDF).IBM.Retrieved2020-09-05.
  6. ^"SSH Frequently Asked Questions".Snailbook.Archivedfrom the original on 2009-01-22.Retrieved2009-06-10.
  7. ^"OpenSSH FAQ".Openssh. 2005-09-20. Archived fromthe originalon 2009-07-10.Retrieved2009-06-10.
  8. ^"Bug 396 – sshd orphans processes when no pty allocated".Bugzilla.mindrot.org.Retrieved2009-06-10.
[edit]