Heinrich
Heinrich

Reputation: 289

Bash - Get output from non-blocking command

I have a python program main.py which produces some outputs which I would like to write to a file. I want to execute the program non-blocking. Usually I would use python2.7 main.py > main.out & or nohup python2.7 main.py &> main.out &.

My problem is, that the program may run very long, and I want to have access to the output created so far before the program terminates. When I execute python2.7 main.py > main.out and I then stop the program with Ctrl-C the output file has the output created so far. However, with the nonblocking commands, the output-file remains empty during execution, and after I kill the program.

How do I do this?

Upvotes: 0

Views: 226

Answers (1)

shyam
shyam

Reputation: 9368

You can try using the -u flag for python which will run the python process with unbuffered stdout/stderr

python2.7 -u main.py > main.out &

Upvotes: 1

Related Questions