Back in the day when I was young and Unix was the new thing, creating a process that did not get killed when you logged out was a challenge. We used the nohup command to protect our persistent processes from the HUP
signal. If we weren't careful, our processes would get killed when we logged off, or even closed the shell we started them from.
Fast forward to today and I find I am surprised that the default appears to be exactly the opposite. On both Ubuntu and Red Hat systems I find that I can put nearly any process in the background, kill the parent shell, log off, anything and it just keeps going. I see the same behavior with Bash scripts, Python scripts and C programs. I get the same behavior from xterm or ssh sessions.
For example in an xterm or ssh window, type:
while [ 1 ]; do date; sleep 10; done > /tmp/out &
Now from another window run
tail -f /tmp/out
Watch it print the date every 10 seconds, then close the original parent shell with Ctrl-D. Still running. Log out and back in. Still running.
Send it a HUP
signal, it instantly dies.
I can exhibit the same behavior with a Python script or a C program. Sleep or not doesn't matter. For example, this ugly C program behaves the same:
#include <stdio.h>
void main() {
while(1) {
printf("*\n");
fflush(stdout);
int i, j, k = 0;
for(i=0; i < 10000; i++) {
for(j=0; j < 100000; j++) {
k += i * j;
}
}
}
}
This is completely counter to the ways of my youth. I guess I just didn't notice when it changed? Any historians out there know when this happened? Does HUP
even get used for this purpose any more?
If indeed this is the current state of things, my question is: How can I arrange for a process to die when the user logs off or gets disconnected?
I've got a hack that involves watching for the ppid
(parent pid) to change but surely there is something more elegant than that.