(一)問題描述:python
nohup 你的程序命令shell
如:centos
nohup python manage.py runserver 0.0.0.0:6789 (此shell窗口1不要關,另外開一個shell窗口2查看nohup.out)緩存
能夠將你的程序放到後臺運行,不至於關閉shell窗口後,程序終止。bash
可是此時的nohup.out中卻沒有記錄任何shell窗口的輸出日誌。app
這是怎麼回事呢???centos7
(二)分析:.net
經過反覆試驗,我發現。日誌
日誌不是不往nohup.out文件中寫,而是日誌信息會被先存到Linux的緩存中去,等到緩存中的數據達到必定量後,纔會寫到nohup.out中去。code
不信,你把nohup python manage.py runserver 0.0.0.0:6789 程序所在shell窗口1用ctrl+c來終止它,你回頭再到shell窗口2中看看nohup.out,是否是有日誌信息了。
ps:不要用kill殺程序,否則看不到從緩存中寫入nohup.out中的日誌信息。
那麼,解決nohup.out中沒有日誌寫入,或者寫入不及時(實時)的方法就是,杜絕緩存機制。
(三)解決方案:
shell窗口1中:
export PYTHONUNBUFFERED=1 nohup python manage.py runserver 0.0.0.0:6789
ps:忽略以下錯誤提示:
nohup: ignoring input and appending output to ‘nohup.out’
shell窗口2中:
此時你再查看nohup.out,是否是有日誌實時寫入了!!!
參考:https://blog.csdn.net/epeaktop/article/details/82665481
ps:以上操做在centos7上進行的,其餘版本不知道有沒有差別,若有不一樣,請留言,若有用,請點贊推薦,多謝。