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.
Run nohup python bgservice.py & to get the script to ignore the hangup signal and keep running. Output will be put in nohup.
Use the write() Function to Print Output to a File in Python. Use the print() Function to Print Output to a File in Python. Use sys. stdout to Print Output to a File in Python.
It will directly put the output in the file you have selected.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With