Istvan
Istvan

Reputation: 8562

How can I kill a process which was reparenting itself to init?

I have a program which reparenting itself to init, so I can't use the following code:

pid=Process.spawn("xxx")
Process.wait(pid) #started xxx with pid, 
                  #but it respawned itself to 
                  #pid2 which has the ppid set to 1

I am not sure if there is a way to trace what is the new pid.

Upvotes: 0

Views: 405

Answers (1)

Andy Ross
Andy Ross

Reputation: 12033

The question is a little malformed. But you can use cgroups to do this. Create a new cgroup mount point for your activity. This will have a "tasks" file containing all the processes in the system. Then you can create subdirectories of this directory, which will automatically get their own (empty) tasks file. Simply write the PID of the process to this new tasks file. It, and all of its children, will always appear in that tasks file and not the one for the rest of the system. The only way to "escape" the cgroup is to have write access to the tasks files.

Yes, this sounds complicated, but it's really not. This is the mechanism systemd uses to track process trees, probably for the same reason you want to: to kill all children of a process, regardless of what happened to their parents.

Obviously a simpler answer to your question might be "use systemd'.

Upvotes: 1

Related Questions