python3實(shí)現(xiàn)windows下同名進(jìn)程監(jiān)控
python3實(shí)現(xiàn)windows下同名進(jìn)程監(jiān)控,供大家參考,具體內(nèi)容如下
公司老版的SVN服務(wù)器的svn服務(wù)經(jīng)常意外關(guān)閉,需要寫個簡單的監(jiān)控腳本監(jiān)控一下;
首先多個SVN服務(wù)使用不同的端口,使用wmic命令查看所有SVN進(jìn)程占用的端口以此來判斷目標(biāo)服務(wù)是否存活,wimc命令如下:
wmic process where caption=”svn.exe” get commandline /value
然后用正則取出標(biāo)準(zhǔn)輸出中的端口,用來比對;
def get_alive_port(program): """ 獲取目標(biāo)程序占用的端口 :param program {string} 目標(biāo)進(jìn)程 :return portlist {list} 目標(biāo)進(jìn)程占用的端口列表 """ cmd = 'wmic process where caption="%s" get commandline /value' % program ps = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) portlist = [] while True: out = ps.stdout.readline() if out: out = out.decode("gb2312") templist = re.findall("[0-9]{4,5}", out) portlist.extend(templist) else: break return portlist
使用監(jiān)控后發(fā)現(xiàn)SVN服務(wù)不意外關(guān)閉了,但是SVN程序被訪問久了占用過大內(nèi)存需要監(jiān)控一下借助psutil來實(shí)現(xiàn);
def howmuch_memory(program): """ 監(jiān)控目標(biāo)進(jìn)程內(nèi)存是否超過閥值,若超過則關(guān)閉 """ cmd = 'wmic process where caption="%s" get processid /value' % program ps = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) pids = [] while True: out = ps.stdout.readline() if out: out = out.decode("gb2312") templist = re.findall("[0-9]{3,6}", out) pids.extend(templist) else: break for pid in pids: try: p = psutil.Process(int(pid)) p_memory = p.memory_info() if int(p_memory.rss / (1024 * 1024)) >= 200: p.kill() except Exception as e: print("出現(xiàn)如下錯誤:{0}".format(e)) continue
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)Zabbix-API監(jiān)控
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)Zabbix-API監(jiān)控,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09Python光學(xué)仿真學(xué)習(xí)Gauss高斯光束在空間中的分布
這篇文章主要介紹了Python光學(xué)仿真學(xué)習(xí)中Gauss高斯光束在空間中的分布理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-10-10如何用Python對數(shù)學(xué)函數(shù)進(jìn)行求值、求偏導(dǎo)
這篇文章主要介紹了如何用Python對數(shù)學(xué)函數(shù)進(jìn)行求值、求偏導(dǎo)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片
這篇文章主要介紹了詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08jupyter notebook使用argparse傳入list參數(shù)
這篇文章主要介紹了jupyter notebook使用argparse傳入list參數(shù),jupyter notebook其實(shí)是可以使用 argparse來調(diào)用參數(shù)的,只要把參數(shù)轉(zhuǎn)為list即可,下面來看看具體的實(shí)現(xiàn)過程吧2022-01-01