views:

71

answers:

4

I am running a python script in background, but why does it still prints to console, even when piped to a file?

I tried the following command:

python script.py &
python script.py > output.txt &

I tried with a simple script:

print "hello world"

With

python script.py &

It still prints to console.

But

python script.py > output.txt &

works as intended and does not print to console.

+3  A: 

Probably it is outputing on stderr. Try this:

python script.py > output.txt 2>&1 &

Alternatively it looks like you might have started a background task that is still running:

python script.py &

Type fg to bring it to the foreground, and then kill it with Ctrl-C.

Mark Byers
now it doesn't print to console anymore, but it doesn't print to output.txt either...
cseric
Mark Byers
yeah, i double-checked, still no output to file..
cseric
A: 
tommieb75
A: 

hello

Do you print to standard output or standard error? those are two different streams.

Program 2> file # pipe standard error
Program 2>&1 # pipe standard error to standard output (join streams)
Program > file 2>&1 # pipe both channels to file
aaa
A: 

Even this is not enough sometimes. Some applications write to /dev/console instead of /dev/stdout or /dev/stderr. Applications that prompt for a password often do this.

dj_segfault