I want to initiate a process from my python script main.py
. Specifically, I want to run the below command:
`nohup python ./myfile.py &`
and the file myfile.py
should continue running, even after the main.py
script exits.
I also wish to get the pid
of the new process.
I tried:
os.spawnl*
os.exec*
subprocess.Popen
and all are terminating the myfile.py
when the main.py
script exits.
Update: Can I use os.startfile
with xdg-open
? Is it the right approach?
Example
a = subprocess.Popen([sys.executable, "nohup /usr/bin/python25 /long_process.py &"],\
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
print a.pid
If I check ps aux | grep long_process
, there is no process running.
long_process.py which keeps on printing some text: no exit.
Am I doing anything wrong here?
python -c 'import subprocess; subprocess.Popen(["sleep", "60"])'
the output ofps
shows that thesleep
keeps running just fine after Python has exited. – Sleety"nohup /usr/bin/python25 ..."
, which cannot work because thepython
executable will look for a script in a file named exactlynohup /usr/bin/...
, which doesn't exist. And since you specifystderr
asPIPE
without ever reading the pipe's content, you never get to see the error message. Lose thenohup
and&
, and simply runsubprocess.Popen([sys.executable, "/.../long_process.py"])
. Also, don't specifystdin
andstderr
as pipes unless you mean it. – Sleetynohup
makes most sense when you start a process from a shell. I don't seeshell=True
in yourPopen
call. – Ostracon