簡體   English   中英

在服務器后台運行 python 腳本並獲取腳本 output

[英]Run python script in background on server and get script output

所以,我需要在服務器上運行 python 腳本,但我希望腳本到 output 輸出到文件中,以便我可以檢查它在完成后做了什么。 但是,如果到服務器的 ssh 隧道中斷或關閉,我還需要腳本繼續運行。 這是我所知道的:

python3 run.py >> out.txt
將 output 正確。

nohup python3 run.py
將 output 正確並避免中斷。 它還將 output 進入 nohup.out 但不會在當前 session 的后台運行(從而阻止我做任何其他事情)。

所以我會認為

nohup python3 run.py >> out.txt &
會為我工作,但它沒有。
我已經嘗試了我想要的不同組合,但無法讓它工作......一旦我添加&它似乎 output 文件就停止了。 我不知道為什么。 任何幫助表示贊賞!

您正在尋找的內容已經在這里得到解答

https://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session

您需要使用tmux使其在后台運行

最簡單的方法是使用&在后台運行您的命令。 然后只寫:

disown -a

對於遇到類似問題的任何人,我猜 nohup 和 python output 緩沖存在問題。
請參閱: Nohup 未將日志寫入 output 文件

但解決方法是:

nohup python3 -u run.py >> out.txt &

(@VPfB 提供的答案)

看看這里

python3 yourscript.py >> out.txt & disown

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM