Kill the current process when it is detached from its parent on Linux

39 Views Asked by At

I have a custom VSCode language server that is started as a child of VSCode (node). There is a bug somewhere that causes it to be detached and keep running when VSCode exits (maybe when it is killed).

I haven't been able to reproduce or diagnose the bug, but as a workaround is there a way I can detect in my process (the one that gets detached) when it is detached from its parent, so that it can kill itself?

1

There are 1 best solutions below

0
Timmmm On

As far as I can tell there aren't any signals when this happens, so the best you can do is poll the parent process ID for changes. When a process is detached its parent ID will change to 1.

You can get the parent ID from the PPid line in /proc/self/status. There might be other ways.