16
votes

I have a python script which takes an input, formats it into a command which calls another script on the server, and then executes using 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 append & to the end because otherscript.pl takes some time to execute, and I prefer to have run in the background. However, the script still seems to execute without giving me back control to the shell, and I have to wait until execution finishes to get back to my prompt. Is there another way to use subprocess to fully run the script in background?

2
Popen is a lot more flexible than straight out call - a bit more overhead but it has the control you're looking for - Mostly Harmless

2 Answers

27
votes

& 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:

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

  2. Using shell=True enables all of the shell's features. Don't do this unless command including thingy 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().

6
votes

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