If you're doing this in C, you need to do a setsid(2)
in your code, along with some fork()
and exit()
calls. setsid()
has this effect:
... creates a new session if the calling process is not a process
group leader. The calling process is the leader of the new
session, the process group leader of the new process group, and has
no controlling tty.
That's from the man page. Basically, if a process that's a process group leader gets certain signals, every process ID in that process group gets the signal. You can see the mechanism for this in the kill(2)
man page. If the PID you call kill()
on is negative, the signal gets sent to every process in the process group.
You also need to fork()
and exit()
in the right places. Basically look at instructions on how to become a daemon process. The parts you need to do:
switch (fork()) {
case -1: return -1;
case 0: break;
default: _exit(EXIT_SUCCESS);
}
setsid();
switch (fork()) {
case -1: return -1;
case 0: break;
default: _exit(EXIT_SUCCESS);
}
Read about becoming a daemon process for more of the rationale behind this code.
setpgid
. Haven't tested, or I'd make this an answer. – derobert Oct 11 '13 at 16:36