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?

  • The question is unclear to me. You're mixing program and process in a confusing way. Is it D that shutdowns? What do you mean by that? – Stéphane Chazelas Feb 8 '15 at 21:24
  • I added a period to clarify. It's all one program. You can launch it from the terminal with restarter -A. It spawns a child process using exec (ie. it calls restarter -D). Now we have two processes, A and D. Process D now sends SIGINT to A, causing A to die. Now D runs exec restarter -A, effectively restarting process A. Now the terminal user can no longer kill the processes with CTRL-C – Ethan Feb 8 '15 at 21:31
  • Note that exec 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. – Stéphane Chazelas Feb 8 '15 at 22:30
  • is it possible to avoid giving up the foreground pg, even with a dead parent? – Ethan Feb 8 '15 at 22:33
  • 1
    you could pipe your process to 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)). – Stéphane Chazelas Feb 8 '15 at 22:39

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.

| improve this answer | |
  • You mean fork a second process (not exec). If you exec a copy of yourself, you will restart, in same process. – ctrl-alt-delor Feb 8 '15 at 23:28
  • sorry, my language is colored by use of golang, which doesn't seem to have fork, only something exec-like – Ethan Feb 9 '15 at 2:45

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.