Skip to content

Instantly share code, notes, and snippets.

@otherwiseguy
Created July 15, 2014 21:40
Show Gist options
  • Save otherwiseguy/6f069f7ba2203864c666 to your computer and use it in GitHub Desktop.
Save otherwiseguy/6f069f7ba2203864c666 to your computer and use it in GitHub Desktop.
An example how in python on linux you can ensure that a signal gets passed to a child on its parent's death
#!/usr/bin/env python
from prctl import prctl, PDEATHSIG # https://pypi.python.org/pypi/prctl/1.0.1
import signal as sig
import subprocess
p = subprocess.Popen(['yes'], preexec_fn=lambda: prctl(PDEATHSIG, sig.SIGTERM))
p.communicate()
@otherwiseguy
Copy link
Author

Running deathsig will start 'yes' which just echoes 'y' forever. Without the preexec_fn, killing deathsig results in yes being reparented to init and continuing to run indefinitely. With it, SIGTERM is sent to the 'yes' child process on parent death, allowing it to terminate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment