I am trying to run a server in python. I need to start the service as a subprocess and after occurrence of an event, I need to stop the server and restart it. The problem is restarting it with a few modifications. The program does terminates the server but does not restart it
Simplified versions of code are below:
Server.py
import time
while(1):
print("Executing...")
time.sleep(1)
Start.py
import time
import subprocess
import signal
import os
def start_service():
p = subprocess.Popen("python3 Server.py", shell = True)
time.sleep(3)
os.killpg(os.getpgid(p.pid),signal.SIGTERM)
print("This is never printed!")
for i in range(3):
start_server()
The output is:
xyz@xyzsystem:~/rougharea/$ python3 Start.py
Executing...
Executing...
Executing...
Terminated
xyz@xyzsystem:~/rougharea/$
It runs only once instead of running 3 times.
Also, print statement just after os.killpg() is not executed.
Can someone tell me what's wrong here?