Skip to content Skip to sidebar Skip to footer

Python: Subprocess Calling A Script Which Runs A Background Process Hanging

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

Solution 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/null2>&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.

Post a Comment for "Python: Subprocess Calling A Script Which Runs A Background Process Hanging"