I have a python script - script.py
, that is running as a system service on ubuntu by the name my.service
. From script.py
I 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)