![](/img/trans.png)
[英]Bash not redirecting Python multiprocessing output to file when run in background
[英]Bash won't redirect output to a file and run in the background
我見過很多與此類似的問題,但問題是其他解決方案都沒有解決我的問題。 我想運行 2 個 python 腳本並使用nohup
將它們的 output 重定向到單獨的日志文件。
此代碼位於名為startmain
的 bash 腳本中:
nohup python3 GUI.py > GUI.log 2>&1 &
nohup python3 main.py > main.log 2>&1 &
當我運行sh startmain
時,腳本都會執行,但沒有任何內容寫入任何一個日志文件。
現在假設我通過刪除行尾的&
字符來更改startmain
代碼:
nohup python3 GUI.py > GUI.log 2>&1
nohup python3 main.py > main.log 2>&1
這段代碼說我不希望腳本在后台運行,所以只運行 GUI.py; 但是,在這里它實際上將所有 output 從 GUI.py 引導到 GUI.log 。 我知道為什么 main.py 不在這里運行,但我不知道為什么 output 在這種情況下被寫入 GUI.log 但當我嘗試在后台運行腳本時卻沒有。
知道我的問題是什么嗎? 我的問題與類似問題的不同之處在於我正在執行 2 個腳本,這可能是它不起作用的原因。
感謝@KamilCuk 的幫助!
添加-u
選項解決了這個問題:
nohup python3 -u GUI.py > GUI.log 2>&1
nohup python3 -u main.py > main.log 2>&1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.