Created
July 9, 2018 13:06
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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