Reputation: 197
I wanted to run execute a python script as a UNIX shell command independently using python Popen
.
Script1.py
: This script will take multiple arguments.
Script2.py
: This script will parse some conditions and will prepare necessary arguments needed to call Script1.py
Assume Script2.py
parsed some conditions and prepared a command to run Script1.py
Command : python Script1.py arg1 arg2 arg3
Currently running the script as :
proc=subprocess.Popen(Command,shell=True,stdout=None, stderr=None)
But, when I kill Script2.py
manually(using CTRL+C), I could see that Script1.py is also getting killed/Terminated.
How can I run Script1.py
independently without worrying about status of Script2.py
?
Upvotes: 4
Views: 4147
Reputation: 99051
The way to detach a process from its parent on window$
is:
from subprocess import Popen
CREATE_NEW_PROCESS_GROUP = 0x00000200
DETACHED_PROCESS = 0x00000008
Popen(['python', 'drive:/path/to/loop_test.py', 'some_arg', 'another_arg'],
stdin=None, stdout=None, stderr=None, shell=True,
creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP
)
On linux
you should be able to achieve the same using nohup
and preexec_fn=os.setpgrp
, i.e.:
Popen(['nohup', 'python', '/path/to/loop_test.py', 'some_arg','another_arg'],
shell=True, stdout=None, stderr=None, preexec_fn=os.setpgrp )
Upvotes: 6