Skip to content

Instantly share code, notes, and snippets.

@AlexDemian
Created July 9, 2018 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDemian/6ef7f0e55e1f553b10df3258b2583dba to your computer and use it in GitHub Desktop.
Save AlexDemian/6ef7f0e55e1f553b10df3258b2583dba to your computer and use it in GitHub Desktop.
Python subprocess: recursive restart of child process(or child process group) example (Ubuntu)
import subprocess
import os
import signal
import time
path = os.path.abspath(__file__).rpartition('/')[0]
child_comm = ['python %s/child.py' % path]
parent_comm = ['python %s/parent.py' % path]
proc = subprocess.Popen(child_comm, shell=True)
print 'New child process created'
your_condition = True
while your_condition:
time.sleep(1)
subprocess.Popen(parent_comm, shell=True, preexec_fn=os.setpgrp)
print 'New wrapper(parent process) created'
print 'Terminating old processgroup...'
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment