linux 使用nohup的時候輸出日誌的同時也輸出該程序的pid

今天在調試程序的時候發現找不到進程pid,用grep查找python進程的時候,發現有很多重名的,不知道該殺死哪一個,這裏可以在用nohup的時候同時把pid輸出到文件裏面,這樣就方便操作進程了,我的命令爲:

nohup python3 -u train.py > output2.log 2>&1 & echo $! > run.pid

這個運行完以後,訓練的日誌會輸出到output2.log中,而進程的id就會輸出到run.pid中,親測可用。

後面如果想停止該程序,就可以直接在run.pid文件裏面找到pid,然後用下面的命令:

kill -9 your_pid

就可以終止程序了

參考文獻

[1]. How to run nohup and write its pid file in a single bash statement. https://stackoverflow.com/questions/20254155/how-to-run-nohup-and-write-its-pid-file-in-a-single-bash-statement

[2].如何獲取進程ID殺死一個nohup的過程?(How to get the process ID to kill a nohup process?). https://www.it1352.com/321596.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章