0

I try to use nohup to record the output of my python script. However, I can see the output to the screen when I am not using nohup.

Start at XX:XX:XX

Finished at XX:XX:XX

But then I use nohup but the nohup.out file is empty. Anyone got a hint of what is going on?

This is the command I used :

nohup python myPython.py &

I tried nohup before and it work perfectly good. I also tried to run some short test python script to record the output and it is good, too.

The test script is short and simple:

string1 = ["John","1","2","0","f","a","s"]
line = [""]*6
line[3:6] = string1 [0:3]
print line
  • did you add a shebang? #!/usr/bin/env python – mikejonesey Nov 10 '16 at 4:30
  • also try: nohup ./myPython.py & rather than nohup myPython.py & – mikejonesey Nov 10 '16 at 4:31
  • Yes I added the shebang already. Actually, I tried with the test script i just use nohup python myPython.py & , and it worked. – Yichao Cai Nov 10 '16 at 4:44
1

Simple answer, the stdout is not flushed automatically.

You can change it to unbuffered mode, e.g

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

See the following link for more details

https://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print

  • Actually, I just got to know that the output is stored in the nohup.out when the script is finished. However, I can see there are output in nohup.out file even it is in the middle of execution. – Yichao Cai Nov 10 '16 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.