8

My understanding is that in order to block a process signal like SIGHUP, you would need to do so from within the process the signal is being sent to. Yet, a Unix shell like bash can spawn a child process and block the HUP signal for the child from within the parent, using the nohup command. How does this work? Does nohup block the signal and then exec the child process without forking? That's the only way I can think of.

7

You can take a look at the source code of an implementation of nohup, e.g. GNU's coreutils version. There's a ton of setup, some of it for internationalisation purposes, the rest to handle the various redirection options; then the actual "nohupping" happens:

  signal (SIGHUP, SIG_IGN);

  char **cmd = argv + optind;
  execvp (*cmd, cmd);

As you surmise, this sets the process up to ignore the HUP signal, then execs the requested command.

-2

The act of using nohup simply changes the PPID of the spawned process to 1 (init) so that when the shell exits, it is no longer a child process of that shell and so therefor doesn't receive a HUP.

EDIT: Should think more before I post sometimes. Leaving it here to remind me of my shame :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.