Issue
nohup python test.py &
In the generated nohup.out file, 'print' output are not written into the it, and some log info are not written the nohup.out either. Is this normal? I just want to leave the program run background and periodically I can check the progress of the program by open the nohup.out. For example, my code has this line:
with open(file_name, 'r', encoding='utf8') :
for index, line in enumerate(f):
logger.info(index)
#print(index)
By opening nohup.out, I want to see the current value of 'index', so that I know how much content it has processed. However, in this nohup.out, I can not see any 'index' info in it. Why is that?
I used to run programs in a similar way and can see the index in the nohup.out sometimes.
What might be wrong for my running?
Solution
Based on running a quick test, I saw printing to stderr happens first, then stdout at the end of my script. I suspect you need to explicitly flush stdout every once in a while or you can print to stderr.
To print to stderr, define this eprint function and call that instead of printing.
import sys
def eprint(*args,**kwrgs):
print(*args,file=sys.stderr,**kwargs)
To flush stdout, do
import sys
sys.stdout.flush()#call this periodically
My test:
#test.py
import sys
for x in range(0,10,2):
sys.stdout.write(str(x)+'\n')
sys.stderr.write(str(x+1)+'\n')
nohup python test.py
$ cat nohup.out
1
3
5
7
9
0
2
4
6
8
$
Answered By - Matt Miguel Answer Checked By - Clifford M. (WPSolving Volunteer)