一時興起就寫了,頭次用這個模塊,入了坑,最后度娘度娘拯救了我,差點爬不起來。
下面來整理下我的誤區(qū):
- os.system(CMD) 返回的是shell執(zhí)行是否成功返回的值
- os.popen(CMD) 返回的是shell執(zhí)行顯示的值,返回的是內存中的編碼
- os.popen(CMD).read() 執(zhí)行命令并讀取內存中的數(shù)據(jù)
直接上代碼
# -*- coding: utf-8 -*-
import os
import requests
import json
def dingTalk(msg_text):
url = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx"
headers = {
"Content-Type": "application/json"
}
data = {
"msgtype": "markdown",
"markdown": {
"title": "Seriver Process Monitor Alert",
"text": msg_text
},
"at": {
"atMobiles": [
"12345678900" #手機號
],
"isAtAll": False #通知全部成員
}
}
json_data = json.dumps(data)
requests.post(url=url, data=json_data, headers=headers)
def Check_Process():
Stat_Laravel_Zz = " ps -A -ostat,ppid,pid,cmd | grep laravel-echo-server | grep -v grep | awk '{print $1}'"
Kill_Laravel_Zz = " ps -A -ostat,ppid,pid,cmd | grep laravel-echo-server | grep -v grep | awk '{print $2}' | xargs kill -9 "
Laravel_echo_server = 'ps -ef | grep -v grep |grep laravel-echo-server|wc -l'
Start_Laravel_echo = 'cd /www/wwwroot/kulianOA2.0/;/usr/local/nodejs/bin/laravel-echo-server start&>>/python_shell_script/laravel-echo-server.log&'
if os.popen(Stat_Laravel_Zz).read() != 'Zz':
if os.popen(Laravel_echo_server).read() < str(1):
os.system(Start_Laravel_echo)
number_process = os.popen(Laravel_echo_server)
process_null_msg = "### 當前Laravel-echo-server進程少于0\n啟動中......\n### 已啟動,當前Laravel-echo-server進程數(shù)有%s"% str(number_process) + '個\n'
dingTalk(process_null_msg)
else:
os.system(Kill_Laravel_Zz)
os.system(Start_Laravel_echo)
number_process = os.popen(Laravel_echo_server)
process_null_msg = "### 當前Laravel-echo-server有僵尸進程\nKILL Laravel-echo-server......\n ### 已啟動,當前Laravel-echo-server進程數(shù)有%s"% str(number_process) + '個\n'
dingTalk(process_null_msg)
if __name__ == "__main__":
Check_Process()