Answer the question
In order to leave comments, you need to log in
How to start a process from a Python service that will continue to live after the service dies?
I made a service in Python that can start and kill third party processes (using Popen
). But there is one small problem: when a service ( systemctl stop
or just a kill
process) exits, it takes running processes with it to the grave.
A couple of examples
pyservice.service
(unit-file):[Service]
ExecStart=/opt/pyservice/service.py
service.py
:...
subprocess.Popen('server.sh')
...
server.sh
:/usr/bin/java -server -jar ./Application.jar &
server.sh
with pyservice
. ps -aef --forest
shows the expected tree:python3.6 /opt/pyservice/service.py
\_ [server.sh] <defunct>
/usr/bin/java -server -jar ./Application.jar
/usr/bin/java -server -jar ./Application.jar
service.py
, Application.jar
it will also end. kill service.py
or restarting pyservice
, our launched through it server.sh
and its contents continue to rustle. os.system
, subprocess.Popen
, nohup
, &
and their arguments. Answer the question
In order to leave comments, you need to log in
the easiest way is to use the system command screen
when the screen itself dies - the running process continues to work
Try a double fork:
https://stackoverflow.com/a/41433401
Example. The ping will continue to work after the parent terminates:
import os, time, sys
import subprocess
time.sleep(1)
forked_pid = os.fork()
if forked_pid == 0:
print("I am child!")
subprocess.Popen(['ping','-c','10','localhost'])
print("Child Quit")
else:
print("I am parent!")
time.sleep(2)
print("Parent Quit")
sys.exit()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question