0

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?

Community
  • 1
  • 1
Yi.He
  • 69
  • 1
  • 7
  • 1
    The main difference with background is that the keyboard is not connected. Normally redirecting stdin covers that problem, but some programs access the keyboard directly rather than using stdin, typically those prompting for a password. Do you have any subprocesses that do that? – cdarke Jun 04 '15 at 16:03
  • Yes, this works.Even though I thought the subprocess do not read from stdin, it actually is. Added an < /dev/null option to supress that and the script runs find in background. Thanks cdarke. – Yi.He Jun 04 '15 at 19:06
  • @Yi.He Can you convert that to an answer and get it out of the unanswered queue? – 4ae1e1 Jun 04 '15 at 19:09
  • Comment added as an answer, with the error message you should have got. – cdarke Jun 04 '15 at 19:15

1 Answers1

2

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.

cdarke
  • 42,728
  • 8
  • 80
  • 84