python指令碼監控進程,服務停止後自動啟動__python

來源:互聯網
上載者:User
執行個體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 每十分鐘運行該指令碼一次

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.