Yi.He
Yi.He

Reputation: 69

Runing a script in background will hang the subprocess

I am running a script that launches

run_app.py >& log.out

In run_app.py, it will start a few subprocesses and will read stdout/stderr of the subprocesses through pipe. I can run the script fine but if I try to put it into background by:

run_app.py >& log.out &

The run_app.py will hang on reading data from subprocess. It seems that it is similar to this thread: ffmpeg hangs when run in background

My subprocess also write a lot which might overflow the PIPE_BUF.

However, I am redirecting&writing my stdout/stderr to a file. Are there any suggestions might prevent hanging when I put the script to background while able to save output in a file instead of redirecting them to /dev/null?

Upvotes: 0

Views: 380

Answers (1)

cdarke
cdarke

Reputation: 44354

When a background process is running, its standard I/O streams are still connected to the screen and keyboard. Processes will be suspended (stopped) if they try to read from the keyboard.

You should have to a message saying something like: Stopped (tty input). That would have been sent to the shell's stderr.

Normally redirecting stdin covers that problem, but some programs access the keyboard directly rather than using stdin, typically those prompting for a password.

Upvotes: 2

Related Questions