Execute Subprocess in Background
If you want to execute it in Background I recommend you to use nohup
output that would normally go to the terminal goes to a file called nohup.out
import subprocess
subprocess.Popen("nohup usr/local/bin/otherscript.pl {0} >/dev/null 2>&1 &", shell=True)
>/dev/null 2>&1 &
will not create output and will redirect to background
&
is a shell feature. If you want it to work with subprocess
, you must specify shell=True
like:
subprocess.call(command, shell=True)
This will allow you to run command in background.
Notes:
Since
shell=True
, the above usescommand
, notcommand_list
.Using
shell=True
enables all of the shell's features. Don't do this unlesscommand
includingthingy
comes from sources that you trust.
Safer Alternative
This alternative still lets you run the command in background but is safe because it uses the default shell=False
:
p = subprocess.Popen(command_list)
After this statement is executed, the command will run in background. If you want to be sure that it has completed, run p.wait()
.