python: subprocess calling a script which runs a b

2019-07-15 12:39发布

问题:

I have an issue using subprocess.call when calling scripts which in turn run background processes.

I am calling a bash script from a python script. python 2.7.3.

#!/bin/python
from subprocess import call
.
.
call(["run_exp",file_name])
print "exp complete!"
.
.

run_exp is a bash script which runs a process in the background.

#!/bin/bash
.
.
run_task auto_output 2>/dev/null &
.
.
echo "run_exp finished!"

The run task command is another bash script. This is always completed by the time run_exp has finished.

Running run_exp from command line I see expected behaviour and all processes finish completed.

An issue arises when i call the run_exp command using python call. When using call I see the output "run_exp finished!" but never "exp complete!". If I remove the run_task operation (and associated code with its operation in run_exp) from run_exp, the call command runs to completion as expected. This leads me to believe there is an issue using call when the script called runs processes in the background.

Can anyone shed any light on why this might occur. Thanks!

回答1:

The output of the background scripts is still going to the same file descriptor as the child script. That's why the parent script is still waiting for it to finish.

You should close all file descriptors in your background scripts if you want to demonize them:

(run_task auto_output >/dev/null 2>&1) &

(The parentheses do this in a subshell which I sometimes found was needed.)

Also it can help to wait explicitly at the end of your child script for the background process:

run_task auto_output 2>/dev/null & backgroundPid=$!
...
echo "run_exp finished!"
wait "$backgroundPid"

And maybe combining both strategies should also tried if both fail alone.