0

I'm trying to run a python script with the nice level set.

nice -n 5 python3 blah.py

runs as expected and sends text output to the screen. However, I would like to pipe the output to a text file and run this all in the background so I can go and check on the progress remotely.

However,

nice -n 5 python3 blah.py > log.txt &

creates the log file log.txt but doesn't write anything to the text file so I'm not sure where the standard output is being sent to or how to direct it to my text file.

218
  • 1,754
  • 7
  • 27
  • 38

2 Answers2

2

I eventually solved this using the command

nice -n 5 python3 -u blah.py >log.txt & 

-u forces the binary I/O layers of stdin, stdout and stderr to be unbuffered. This allows the output of the python script to be written to the text file whilst the process is running.

218
  • 1,754
  • 7
  • 27
  • 38
0

I'm guessing you're running the command via ssh and want to log out between running and checking the log. To do this run:

nohup nice -n 5 python3 blah.py > log.txt &

This will prevent killing the program on logout. As well nohup redirects stderr to stdout, which also might be what's causing an empty log.txt file.

rzymek
  • 9,064
  • 2
  • 45
  • 59
  • This still doesn't write to the log file whilst the process is running because without -u (see answer below) the output of stdin, stdout and stderr are bufferred. nohup will be useful for the purposes of running this over an ssh connection. – 218 Nov 14 '13 at 09:45