Subprocess execution in background

I have a python script that takes input, formats it into a command that calls another script on the server, and then executes using a subprocess:

import sys, subprocess thingy = sys.argv[1] command = 'usr/local/bin/otherscript.pl {0} &'.format(thingy) command_list = command.split() subprocess.call(command_list) 

I add & to the end, because otherscript.pl takes some time to execute, and I prefer to run in the background. However, the script still works without returning control of the shell to me, and I need to wait until the execution completes to return to my prompt. Is there any other way to use subprocess to fully run a script in the background?

+8
source share
2 answers

& is a shell function. If you want it to work with subprocess , you must specify shell=True as:

 subprocess.call(command, shell=True) 

This will allow you to execute the command in the background.

Notes:

  • Since shell=True , the above example uses command , not command_list .

  • Using shell=True allows you to use all the shell functions. Do not do this if command including thingy does not come from sources you trust.

Safer alternative

This alternative still allows you to run the command in the background, but it is safe because it uses shell=False by default:

 p = subprocess.Popen(command_list) 

After executing this statement, the command will be launched in the background. If you want to be sure that it is complete, run p.wait() .

+16
source

If you want to run it in the background, I recommend that you use the nohup output, which usually goes to the terminal and 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 generate output and redirect to the background

0
source

All Articles