Python3判断shell下进程是否存在&&启动&&邮件通知
判断进程是否存在
def isRunning(process_name):
try:
process = len(os.popen('ps aux | grep "' + process_name + '" | grep -v grep').readlines())
if process >= 1:
return True
else:
return False
except:
print("Check process ERROR!!!")
return False
启动挂掉的进程
def startProcess(process_script):
try:
result_code = os.system(process_script)
if result_code == 0:
return True
else:
return False
except:
print("Process start Error!!!")
return False
由于脚本层层调用,如需发现异常一定要查看各种日志。。。。。
crontab 和 shell命令下会有各种环境变量不一致的问题。。。。。
绊倒过N次了。。。。今天又被绊了。。。。
例子:实现监控某个进程,如果进程挂掉,则启动进程。
并且接着发邮件通知。。。
#!/bin/env python3
# -*- coding: utf-8 -*- from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody
import sys, time
import os def log(logfile, content):
f = open(logfile, 'a')
f.write(time.strftime("\n%Y-%m-%d %H:%M:%S ") + content)
f.flush()
f.close() def Email(to, subject, body):
creds = Credentials(
username='xxxxxx',
password='xxxxxx'
)
account = Account(
primary_smtp_address='xxxxxx',
credentials=creds,
autodiscover=True,
access_type=DELEGATE
)
m = Message(
account=account,
subject=subject,
body=HTMLBody(body),
to_recipients = [Mailbox(email_address=to)]
)
m.send() def isRunning(process_name):
try:
process = len(os.popen('ps aux | grep "' + process_name + '" | grep -v grep').readlines())
if process >= 1:
return True
else:
return False
except:
print("Check process ERROR!!!")
return False def startProcess(process_script):
try:
result_code = os.system(process_script)
if result_code == 0:
return True
else:
return False
except:
print("Process start Error!!!")
return False if __name__ == '__main__': process_name = "spark-streaming"
process_script = "/bin/bash /home/admin/agent/spark/streaming_start.sh" subject = "datacollect-1 spark-streaming ERROR"
logfile = "/home/admin/bin/logfile.log" content = ""
wrong_to = "zhzhang09@126.com"
sleep = 1 content = "There are %d arguments, They are %s" % (len(sys.argv), str(sys.argv))
log(logfile, content) if len(sys.argv) == 3:
user = sys.argv[1]
to = sys.argv[2]
log(logfile, content)
time.sleep(sleep)
isrunning = isRunning(process_name)
if isrunning == False:
content = "spark-streaming running ERROR \n"
log(logfile, content)
Email(to, subject, content)
isstart = startProcess(process_script)
time.sleep(sleep)
if isstart == True:
content += "spark-streaming start SUCCESS \n"
log(logfile, content)
Email(to, subject+" && start SUCCESS", content)
else:
log(logfile, "running ERROR")
脚本执行方法:
/usr/local/bin/python3 /home/admin/bin/sparkStreamingEmail.py zhzhang09@126.com zhzhang09@126.com
未完待续。。。
Python3判断shell下进程是否存在&&启动&&邮件通知的更多相关文章
- 启动bash shell的三种方式下,检查的启动文件
启动bash shell的三种方式 1.登录时当做默认登录shell 2.作为非登录shell的交互式shell 3.作为运行脚本的非交互shell 一.登录shell 登录Linux系统时,bash ...
- linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行
linux shell编程,先等10秒再判断是否有进程存在,存在就再等10秒再杀了进程才运行 crontab每分钟执行一次,但5秒以上才有更新数据,有时候一分钟可能跑不完上一个进程,需要先等10秒再判 ...
- [转]使用 Shell 对进程资源进行监控
原文:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-monitoring/ 使用 Shell 对进程资源进行监控 检查进程是否存在 在 对 ...
- shell查看进程
用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]th ...
- python3之线程与进程
1.CPU运行原理 我们都知道CPU的根本任务就是执行指令,对计算机来说最终都是一串由“0”和“1”组成的序列.CPU从逻辑上可以划分成3个模块,分别是控制单元.运算单元和存储单元,这三部分由CPU内 ...
- shell浅谈之九子shell与进程处理
转自:http://blog.csdn.net/taiyang1987912/article/details/39529291 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] ...
- Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...
- 【Linux下进程机制】从一道面试题谈linux下fork的运行机制
今天一位朋友去一个不错的外企面试linux开发职位,面试官出了一个如下的题目: 给出如下C程序,在linux下使用gcc编译: #include "stdio.h" #includ ...
- linux下进程相关操作
一.定义和理解 狭义定义:进程是正在运行的程序的实例. 广义定义:进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动. 进程的概念主要有两点: 第一,进程是一个实体.每一个进程都有它自己的 ...
随机推荐
- aligned_storage简单学习
#include <iostream> #include <type_traits> #include <string> /* template< std:: ...
- linux shell 脚本攻略学习13--file命令详解,diff命令详解
一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...
- Selenium WebDriver使用IE浏览器 属性设置
System.setProperty("webdriver.ie.driver", "D:\\developsoft\\Selenium_Test\\IEDriverSe ...
- 进阶之路(基础篇) - 021 arduino基础知识
什么是arduino(翻译自arduino官方介绍)Arduino 是一款便捷灵活.方便上手的开源电子原型平台,包含硬件(各种型号的arduino板)和软件(arduino IDE).她适用于艺术家. ...
- 安卓高手之路之ClassLoader(二)
因为ClassLoader一定与虚拟机的启动有关系,那么必须从Zygote的启动开始看代码.下面就分析一下这些代码,行数不多: int main(int argc, const char* const ...
- [AaronYang风格]微软Unity2.X系统学习笔记,记录
读者约定: Unity我直接简写U了 Unity Dependency Injection(DI) 欢迎学习Unity,通过学完下面的几个流程的引导,你应该就可以很顺利的应用Unity到你的项目中去了 ...
- SQL中实现SPLIT函数几种方法
例1 代码如下 复制代码 create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp ...
- 《自己动手写框架2》:用200行的DBF解析器来展示良好架构设计
因为工作关系.须要工作其中,须要读取DBF文件.找了一些DBF读取开源软件,要么是太过庞大,动不动就上万行.要么是功能有问题,编码,长度,总之是没有找到一个很爽的. 在万般无奈之下,我老人家怒从心头起 ...
- python md5 问题(TypeError: Unicode-objects must be encoded before hashing)
import hashlib import sys def md5s(): m=hashlib.md5() strs=sys.argv[1] m.update(strs.encode("ut ...
- MySQL开启federated引擎实现数据库表映射
1.查看federated引擎是否开启 点击进入Navicat并点击键盘上F6,出现命令行界面 ,输入指令:show engines; 2.开启federated引擎 Windows系统 : 在my. ...