執行個體1:
點擊(此處)摺疊或開啟 #!/usr/bin/env python
import subprocess
import datetime
import re
import sys
import os
res = subprocess.Popen("id root",stdout=subprocess.PIPE,shell=True)
output_lines=res.stdout.readlines()
for line in output_lines:
if not re.search("ma", line):
line = line.strip()
dt=datetime.datetime.now()
fp=open('/root/check_ma_group/check_ma.txt','a')
fp.write('root in ma group at %s\n' % dt.strftime('%Y-%m-%d %H:%M:%S'))
fp.close()
subprocess.Popen("/usr/sbin/usermod -G ma root",shell=True)
else:
pass
執行個體2:
點擊(此處)摺疊或開啟 # -*- coding: utf-8 -*-
#!/usr/bin/python
import os
import subprocess
res = subprocess.Popen('ps -ef | grep ASRS',stdout=subprocess.PIPE,shell=True)
attn=res.stdout.readlines()
counts=len(attn) #擷取ASRS下的進程個數
print counts
if counts<10: #當進程不夠正常啟動並執行個數時,說明某隻程式退出了
os.system('python /home/pi/ASRS/ASRS.py') ##啟動程式
##os.system('reboot') #重啟系統
執行個體三:
點擊(此處)摺疊或開啟 #!/usr/bin/python
import subprocess
import datetime
res = subprocess.Popen(“ps -ef | grep tomcat”,stdout=subprocess.PIPE,shell=True)
tomcats=res.stdout.readlines()
counts=len(tomcats)
if counts<4:
dt=datetime.datetime.now()
fp=open(‘/root/tomcat6.txt’,
定時運行該指令碼
crontab -e
點擊(此處)摺疊或開啟 */10 * * * * python /root/autorestart-tomcat.py 每十分鐘運行該指令碼一次