-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitro.py
52 lines (52 loc) · 1.74 KB
/
monitro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
0 import smtplib
1 import time
2 import psutil
3 from email.mime.text import MIMEText
4 from email.header import Header
5 6
7 def sendmail(receivers=['[email protected]']):
8
9 # 第三方 SMTP 服务
10 mail_host = "smtp.163.com" # 设置服务器 不要用QQ邮箱
11 mail_user = "[email protected]" # 用户名
12 mail_pass = "password" # 口令
13 # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
14
15 # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
16 message = MIMEText('进程已经关闭,请注意!', 'plain', 'utf-8')
17 message['From'] = "{}".format(mail_user)
18 message['To'] = ",".join(receivers)
19
20 subject = '监控进程'
21 message['Subject'] = Header(subject, 'utf-8')
22
23 try:
24
25 smtpObj=smtplib.SMTP_SSL(mail_host, 465) # 25 为 SMTP 端口号
26 smtpObj.login(mail_user, mail_pass)
27 smtpObj.sendmail(mail_user,receivers,message.as_string())
28
29 print("邮件发送成功")
30 except smtplib.SMTPException as e:
31 print("Error: 无法发送邮件",e)
32
33 def monitor(taskname='python',username='gdcy'):
34 while True:
35 pid = psutil.pids()
36 found=False
37 for k, i in enumerate(pid):
38 try:
39 proc = psutil.Process(i)
40 if proc.name()==taskname and proc.username()==username:
41 found=True
42
43 except psutil.AccessDenied:
44 print("psutil.AccessDenied")
45 if found:
46 time.sleep(1)
47 else:
48 sendmail(['[email protected]'])
49 break
50
51
52 monitor(taskname='gedit',username='gdcy')