在Linux中,可以使用nohup將腳本放置后台運行,如下:
nohup python myscript.py params1 > nohup.out 2>&1 &
- 1
但直接使用上面代碼,無法在程序運行過程中查看Python中的print "computing" 輸出結果,比如在每次循環中使用print語句等。原因是python的輸出有緩沖,導致nohup.out不能夠馬上看到輸出。
解決方法:
- 使用-u參數,使得python不啟用緩沖。
修改命令如下:
nohup python -u myscript.py params1 > nohup.out 2>&1 &
- 1
- 2
這樣就可以同步看到輸出結果了。
