導航:首頁 > 編程語言 > python持續運行

python持續運行

發布時間:2022-09-28 19:33:26

Ⅰ 如何在Windows下編寫在後台一直運行的python程序

使用python自帶的gui就可以實現,python自帶tk,使用tk的mainloop就會讓python程序一直運行,只要讓gui窗口一直不顯示就是一直在後動員運行了。代碼示意如下:
from
tkinter
import
*
root
=
tk()
root.withdraw()
#
隱藏窗口
root.mainloop()
#
消息循環
把代碼以pyw擴展名保存,執行一下,就會讓程序一直在後台執行,可以通過任務管理器結束,如下:

Ⅱ 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

稍後我會把實例代碼上傳到資料共享裡面。

Ⅲ 如何在Windows下編寫在後台一直運行的Python程序

使用python自帶的GUI就可以實現,python自帶TK,使用tk的mainloop就會讓python程序一直運行,只要讓GUI窗口一直不顯示就是一直在後動員運行了。

代碼示意如下:

from tkinter import *

root = Tk()

root.withdraw() # 隱藏窗口

root.mainloop() # 消息循環

把代碼以pyw擴展名保存,執行一下,就會讓程序一直在後台執行,可以通過任務管理器結束,如下:

閱讀全文

與python持續運行相關的資料

熱點內容
ios支持的解壓縮格式 瀏覽:703
平安經營貸結清後如何解壓 瀏覽:938
蘋果系統的解壓縮軟體 瀏覽:856
python火鍋店運營分析 瀏覽:985
c語言編譯器手機在線 瀏覽:848
戰艦世界什麼伺服器地址 瀏覽:550
windowsphone解壓縮 瀏覽:646
android工程目錄結構 瀏覽:137
pdf文檔是反的 瀏覽:528
javaobject比較 瀏覽:867
安卓如何設置微信屏幕鎖 瀏覽:189
本溪雲伺服器 瀏覽:375
玩機技巧華為app如何了解純凈模式 瀏覽:905
換演算法則數不變 瀏覽:719
java工作流activiti 瀏覽:788
單片機自動門程序 瀏覽:423
java培訓長沙 瀏覽:494
程序員生存現狀 瀏覽:588
光環游戲安裝器在哪個文件夾 瀏覽:654
公眾號圖片被壓縮 瀏覽:291