0

I have a python script - script.py, that is running as a system service on ubuntu by the name my.service. From script.pyI want to launch another python script called reset.py as a separate child process that does some activities and restarts the main service. The problem that I am facing is that whenever the restart command is executed the child script exits prematurely. As per the logging given below, the statement hello occurs in the logs but bellow never occurs.

The content of reset.py is as follows:

logging.info("hello")
subprocess.run(["systemctl", "restart", "my.service"])
logging.info("bellow")

I have tried os.fork(), nohup etc from parent program but nothing works. As soon as the service restarts the child process dies. what can I do to ensure that the spawned child keeps executing even after it restarts its parent?

Commands tried

command = "nohup" + " " + sys.executable + " " + script_path + " &"
subprocess.Popen(command.split(), stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
pid = os.fork()
if pid == 0:
    command = "nohup" + " " + sys.executable + " " + script_path + " &"
    subprocess.Popen(command.split(), stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
Rajdeep Rath
  • 81
  • 2
  • 9
  • Have you read this? https://stackoverflow.com/questions/24787106/run-a-child-process-free-of-the-parent-in-python – Ghoti Jul 02 '21 at 19:06
  • 1
    @Ghoti yes i have read that. I have now edited the question to share what i have tried. It did not work for me – Rajdeep Rath Jul 02 '21 at 19:31

0 Answers0