WebFaction
Community site: login faq

Ok, this might be a basic unix mistake, but my problem is a simple script that runs fine normally to stdout or redirected, but when the output is redirected and the process run in the background, I get nothing.

My simple script test.py:

from time import sleep
i = 0
while 1:
    print i
    i = i+1
    sleep(1)

Output from the commandline:

$ python test.py
0
1
2
3
4
5

Simple redirection to a file works too.

$ python test.py > output.txt
^C
$ cat output.txt
0
1
2
3

However, if i run it in the background, redirecting output to a file, I get nothing:

$ python test.py > output.txt &
[1] 17381
$ tail -f output.txt
$ cat output.txt
$

Nothing... What's going on here?

asked 27 May '11, 19:24

Jakob
3236
accept rate: 0%


Use this instead:

python -u test.py > output.txt &

Python will buffer your output by default, and simply killing the script doesn't immediately flush that standard output to disk

You also have the option of using unbuffered stdout within your script. If you use this a lot, this approach is better.

That said, if you're going to go through that trouble, the standard logging module makes more sense since it is easier to use and more robust, as Sean F suggested.

permanent link

answered 27 May '11, 20:26

ryans ♦♦
5.0k103460
accept rate: 43%

edited 27 May '11, 20:31

After testing this myself, I've reached the conclusion that output redirection only works when your script actually outputs something. If you run it as a background process that produces no output, then there's nothing to redirect.

If you bring your background process to the foreground and then interrupt it with CTRL-C, then output.txt will contain the expected output.

There might be some other bash incantation you can use to get the result you're trying to achieve, but so far I haven't found it.

If you want your script to run in the background and write to a file that you can watch while the script runs, then use I recommend that you use Python's file I/O functions, or perhaps its logging functions, for that purpose.

Hope that helps!

permanent link

answered 27 May '11, 19:56

seanf
12.2k42136
accept rate: 37%

edited 27 May '11, 20:03

Thanks for testing this. I don't know if that makes sense tho, when I run it in the background with no redirect, it still output the numbers to stdout, so redirect should still work, no?

(27 May '11, 20:07) Jakob
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×76
×4
×1

question asked: 27 May '11, 19:24

question was seen: 19,015 times

last updated: 27 May '11, 20:31

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM