I wrote a program. It starts a process (call it A) that spawns a child process (call it D) that shutdowns and restarts A. Problem is, now I can't kill A nicely from the terminal (ie. CTRL-C isn't getting to it). The pgid's of A and D are the same, but it looks like the terminal drops that process group as its foreground, which is why I can't send it signals now. I suspect this happens when the parent process originally dies. Is there some way to prevent that? Can I change the foreground pgid so it looks like the child (D) is actually the parent and the terminal doesn't drop the process group?
1 Answer
A simple enough solution:
Have process A exec a second process A first (call it A'). Then let A block forever. A' can start D, and D can restart A', and A sticks around the whole time as the parent.
-
You mean fork a second process (not exec). If you exec a copy of yourself, you will restart, in same process. Feb 8, 2015 at 23:28
-
sorry, my language is colored by use of golang, which doesn't seem to have fork, only something exec-like– EthanFeb 9, 2015 at 2:45
D
that shutdowns? What do you mean by that?restarter -A
. It spawns a child process using exec (ie. it callsrestarter -D
). Now we have two processes, A and D. Process D now sends SIGINT to A, causing A to die. Now D runs execrestarter -A
, effectively restarting process A. Now the terminal user can no longer kill the processes with CTRL-Cexec
doesn't spawn a process. The shell is waiting for the first process, once that dies, the shell sets the foreground process group back to the shell's. You'd need your first process not to die or be suspended. Probably better rethink the whole thing. If D is a monitoring process, it should be the parent.cat
(restarter -A | cat
).cat
would not die until the pipe is closed (that is when all the processes have died (unless they close their stdout)).