python守護
Ⅰ ubuntu python怎麼作為守護進程一直運行
測試程序
先寫一個測試程序,用於輸出日誌和列印到控制台。
#-*- coding: utf-8 -*-
import logging
import time
from logging.handlers import RotatingFileHandler
def func():
init_log()
while True:
print "output to the console"
logging.debug("output the debug log")
logging.info("output the info log")
time.sleep(3);
def init_log():
logging.getLogger().setLevel(logging.DEBUG)
console = logging.StreamHandler()
console.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
console.setFormatter(formatter)
logging.getLogger().addHandler(console)
# add log ratate
Rthandler = RotatingFileHandler("backend_run.log", maxBytes=10 * 1024 * 1024, backupCount=100,
encoding="gbk")
Rthandler.setLevel(logging.INFO)
# formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
Rthandler.setFormatter(formatter)
logging.getLogger().addHandler(Rthandler)
if __name__ == '__main__':
func()
後台啟動Python腳本
可以使用下面的命令來啟動上面的腳本,讓Python在後台運行。
nohup python -u main.py > test.out 2>&1 &
來解釋一下這幾個命令的參數。這一段來自
其中 0、1、2分別代表如下含義:
0 – stdin (standard input)
1 – stdout (standard output)
2 – stderr (standard error)
nohup python -u main.py > test.out 2>&1 &
nohup+最後面的& 是讓命令在後台執行
>out.log 是將信息輸出到out.log日誌中
2>&1 是將標准錯誤信息轉變成標准輸出,這樣就可以將錯誤信息輸出到out.log 日誌裡面來。
運行命令後,會返回一個pid。像下面這樣:
[1] 9208
後續可以學習Hadoop它們,把pid存起來,到時候stop的時候就把它殺掉。
跟蹤輸出文件變化
為了驗證腳本可以在後台繼續運行,我們退出當前會話。然後重新連接一個Session,然後輸入下面的命令來跟蹤文件的輸出:
tail -f test.out
輸出內容如下:
output to the console
2017-03-21 20:15:02,632 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:02,632 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:05,635 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:05,636 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:08,637 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:08,638 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:11,640 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:11,642 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:14,647 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:14,647 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:17,653 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:17,654 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:20,655 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:20,656 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:23,661 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:23,661 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:26,665 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:26,666 main.py[line:12] INFO output the info log
output to the console
2017-03-21 20:15:29,670 main.py[line:11] DEBUG output the debug log
2017-03-21 20:15:29,671 main.py[line:12] INFO output the info log
說明我們的腳本確實在後台持續運行。
結束程序
可以直接通過之前的那個pid殺掉腳本,或者可以通過下面的命令查找pid。
ps -ef | grep python
輸出的內容如下:
root 1659 1 0 17:40 ? 00:00:00 /usr/bin/python /usr/lib/python2.6/site-packages/ambari_agent/AmbariAgent.py start
root 1921 1659 0 17:40 ? 00:00:48 /usr/bin/python /usr/lib/python2.6/site-packages/ambari_agent/main.py start
user 8866 8187 0 20:03 ? 00:00:06 /usr/bin/python3 /usr/bin/update-manager --no-update --no-focus-on-map
root 9208 8132 0 20:12 pts/16 00:00:00 python -u main.py
root 9358 8132 0 20:17 pts/16 00:00:00 grep --color=auto python
可以看到我們的pid是9208,調用kill殺掉就可以了。
kill -9 9208
編寫啟動及停止腳本
啟動腳本
#!/bin/sh
pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`
if [ "$pid" != "" ]
then
echo "main.py already run, stop it first"
kill -9 ${pid}
fi
echo "starting now..."
nohup python -u main.py > test.out 2>&1 &
pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`
echo ${pid} > pid.out
echo "main.py started at pid: "${pid}
停止腳本
#!/bin/sh
pid=`ps -ef|grep "python -u main.py"| grep -v "grep"|awk '{print $2}'`
if [ "$pid" != "" ]
then
kill -9 ${pid}
echo "stop main.py complete"
else
echo "main.py is not run, there's no need to stop it"
fi
稍後我會把實例代碼上傳到資料共享裡面。
Ⅱ 關於python多線程的一些問題。
創建的子線程默認是非守護的。
非守護:當主線程結束時,子線程繼續運行,二者互不影響。
子線程是守護線程:當主線程結束時,子線程也結束(不管子線程工作有沒有完成)。
join作用是線程同步,是讓主線程等待子線程結束才結束(主線程完成工作了也不結束,阻塞等待,等子線程完成其工作才一起結束)。
相信此時你已經懂你的兩個問題了。
沒加join的時候主線程結束了,所以命令提示符>>>就出來了,可是子線程還沒結束,過了3/5秒後列印了字元串。加了join後主線程等兩個子線程都結束才一起結束,所以最後才出來>>>。
理解確實有點偏差。守護是指子線程守護著主線程,你死我也死,謂之守護。