一个监视服务器运行程序的脚本

一个监视服务器运行程序的脚本

SCFT的计算往往需要很长的计算时间,为了提高效率,我们希望能够在程序计算完成之后通过邮件提醒我们来进行下一步的操作。课题组的服务器虽然不能连接外网,但是可以连接复旦邮箱的服务器通过复旦学邮进行邮件的发送。

import smtplib
import os
import time
import threading

def mail(item):
    mail_host="mail.fudan.edu.cn"
    mail_user="Username"
    mail_pass="Password" 
        
    sender = 'Sender'
    receivers = ['Receiver']
        
    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 25)
        smtpObj.login(mail_user,mail_pass)  
        smtpObj.sendmail(sender, receivers, 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: base64\nSubject: =?utf-8?q?'+item+' is finished!?=\naGVsbG8=')
    except smtplib.SMTPException:
        print('error')

def tiktock():
    file=open('log','a')
    file.write(time.asctime(time.localtime(time.time()))+'\n')
    message = os.popen("rsh c0103 'ps -A w|grep 'a0\.[0-9]*b0\.[0-9]*x[0-9]\.[0-9]*''").read().decode()
    files=[i.split(' ')[-1] for i in message.split('\n') if i]
    for item in files:
        if not item in runningfiles:
            runningfiles.append(item)
            file.write(item+' added\n')
    for item in runningfiles:
        if not item in files:
            runningfiles.remove(item)
            mail(item)
            file.write(item+' removed\n')
    file.close()
    timer=threading.Timer(300,tiktock)
    timer.start()

if __name__ == '__main__':
    runningfiles=[]
    tiktock()

该程序每5分钟检测一次正在运行中的程序并进行比较,如果有程序跑完了就自动发送邮件提醒。
如果你不想在服务器上运行,你也可以运行本地版,利用SSH服务检测。

import threading
import paramiko
import time

def mail(filename):
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    # 第三方 SMTP 服务
    mail_host="Server"
    mail_user="Username"
    mail_pass="Password"
    
    sender = 'Sender'
    receivers = ['Receiver']
    
    message = MIMEText(filename + ' is finished', 'plain', 'utf-8')
    
    subject = filename + ' is finished'
    message['Subject'] = Header(subject, 'utf-8')
    
    try:
        smtpObj = smtplib.SMTP() 
        smtpObj.connect(mail_host, 25)
        smtpObj.login(mail_user,mail_pass)  
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("successfully sent")
    except smtplib.SMTPException:
        print("Error")


def tiktock():
    print(time.asctime( time.localtime(time.time())))
    stdin, stdout, stderr = ssh.exec_command("rsh c0103 'ps -A w|grep 'a0\.[0-9]*b0\.[0-9]*x[0-9]\.[0-9]*''")
    message = stdout.read().decode().split('\n')
    files=[i.split(' ')[-1] for i in message if i]
    for item in files:
        if not item in runningfiles:
            runningfiles.append(item)
            print(item,' added')
    for item in runningfiles:
        if not item in files:
            runningfiles.remove(item)
            mail(item)
            print(item,' removed')
    timer=threading.Timer(300,tiktock)
    timer.start()

if __name__ == '__main__':
    runningfiles=[]

    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname='Server',port=22,username='Username')

    tiktock()

发表评论