0

Context:

I am running a process from bash without & and any redirection i.e like ./foo. The process is running while(1) i.e it is running forever. Also, the process is ignoring SIGHUP i.e not terminating when it gets it.

When I send SIGHUP to the bash process, it also sends a SIGHUP to my process. My process, in turn, just logs the signal and keeps on doing its job.

My Understanding:

I have built my understanding of SIGHUP from the answer here. What I understand is, in my case, bash process should be blocked and not terminate.

Problem:

But it is not the case. bash process does terminate while my process keeps on. But now, instead of bash process, /lib/systemd/systemd --user becomes the new parent.

Environment and Other Details:

Linux lap-0117 5.4.0-87-generic #98~18.04.1-Ubuntu SMP Wed Sep 22 10:45:04 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
AdminBee
  • 22,803
Saad
  • 9

1 Answers1

0

That is normal.

The shell terminates, as SIGHUP is normally sent to a shell when the controlling terminal goes away (i.e. that is a resource revocation interface, one of the very few we have in Unix), and a shell without a terminal is pointless.

The shell itself handles SIGHUP, and the signal handler forwards the SIGHUP to all background processes so they, too, are informed that the controlling terminal has gone away. Any processes that leave the default handler will terminate here, typically these are shell commands that require the terminal.

For example, this mechanism cleans up when you use tail -f to watch a logfile and then close the Terminal window. Both the shell and the tail command running inside it just became useless as they have no terminal to interact with, so they are terminated as well.

Normally, "orphaned" processes are attached to PID 1, as that is the only process that can at any time be expected to handle SIGCHLD and collect the exit code of a child process. Presumably, systemd does something interesting here to have it attached to the user instance of systemd instead, which also fulfills this contract.