使用 nohup 後台運行 python,print 沒有輸出到日誌
nohup python foobar.py > foobar.log 2>&1 &
發現 foobar.log 中顯示不出來 python 程式中 print 的東西。這是因為 python 的輸出有緩衝,導致 foobar.log 並不能夠馬上看到輸出。python 有個 - u 參數,使得 python 不啟用緩衝。
nohup python -u foobar.py > foobar.log 2>&1 &
其他玩法:只輸出錯誤到日誌 # Only error write to log nohup python -u ./foobar.py> /dev/null 2>foobar.log & 不輸出到日誌 # Nothing to Display nohup python -u ./foobar.py> /dev/null 2>&1 & 全部 print 輸出到日誌 # Write all to log nohup python -u ./foobar.py> ./foobar.log 2>&1 &
https://blog.csdn.net/Statham\_stone/article/details/78290813