#!/usr/bin/env python
#!coding=utf-8
import os
import time
import sys
import smtplib
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart def sendsimplemail (warning):
msg = MIMEText(warning)
msg['Subject'] = 'python first mail'
msg['From'] = 'root@localhost'
try:
smtp = smtplib.SMTP()
smtp.connect(r'pop.qq.com')
smtp.login('[email protected]', 'passwd')
smtp.sendmail('[email protected]', ['[email protected]'], msg.as_string())
smtp.close()
except Exception, e:
print e time_str = time.strftime( "%Y-%m-%d", time.localtime( ) )
file_name = "./" + time_str + ".log" while True:
print_str = "";
Pro_status = os.popen('netstat -tulnp | grep nginx','r').readlines()
now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
try:
if Pro_status == []:
os.system('service nginx start')
new_Pro_status = os.popen('netstat -tulnp | grep nginx','r').readlines()
str1 = ''.join(new_Pro_status)
port = str1.split()[3].split(':')[-1]
if port != '9999':
print_str = print_str + "nginx start fail ..."
else:
print_str = print_str + "nginx Program abort. an internal error has occurred" + '\n'
print_str = print_str + "nginx start success ..."
# sendsimplemail(warning = "This is a warning!!!")
else:
print_str = print_str + now_time +"nginx running ..."
time.sleep(3) if os.path.exists ( file_name ) == False :
os.mknod( file_name )
handle = open ( file_name , "w" )
# print ( print_str + "**********1")
try:
handle.write( print_str)
except:
log.error('write backup error:')
finally:
handle.close()
# print ( print_str + "**********2")
else:
handle = open ( file_name , "a" )
print ( print_str + "**********3")
handle.write( print_str + '\n') except KeyboardInterrupt:
sys.exit('\n')

这个是jkport更新版, 旧版在这里:python监控端口脚本

新增日志输出功能

运行模式, 直接守护:nohup ./jkport.py &

不用像以前那么繁琐。

05-08 08:11