How does nohup work?

I have used nohup(1) for years to startup processes, and to ensure they keep running when my shell exits. When a shell exits, each child process will receive a SIGHUP signal, which causes the process to exit if a signal handler is not installed to deal with the SIGHUP signal. When a command is invoked with the nohup(1) utility, the signal disposition for SIGHUP is set to ignored, allowing the process to continue executing when the shell exits. We can see this with the Solaris “psig” command:

$ ssh oscar &
[1] 958

$ psig 958 | grep HUP
HUP default

The psig utility indicates that the SIGHUP disposition is set to the default, which will cause the process to terminate when we exit the shell. When the same command is invoked with the nohup utility, we can see that the signal disposition for SIGHUP is set to ignored:

$ nohup ssh oscar &
[2] 967

$ psig 967 | grep HUP
HUP ignored

Solaris is an amazing Operating system, and allows the signal dispositions of running processes (and process groups!!) to be set on the fly. This is accomplished with nohup’s “-p” and “-g” options:

$ ssh -p 443 oscar &
[1] 1081

$ psig 1081 | grep HUP
HUP default

$ nohup -p 1081
Sending output to nohup.out

$ psig 1081 | grep HUP
HUP ignored

While this isn’t the best example, hopefully you get the point. Sessions, process groups, process group leaders and controlling terminals are really neat concepts, and explained on pages 677 - 700 of Solaris Systems Programming (ISBN: 0201750392). This is an INCREDIBLE book, and sits next to my lazy boy for easy reference.

This article was posted by Matty on 2005-03-01 23:29:00 -0400 -0400