วันพุธที่ 15 เมษายน พ.ศ. 2558

[HOW TO] Check status service linux with Python

ไม่มีความคิดเห็น
เช็คสถานะ service linux ด้วย Python(หัดทำ)
    

     เพื่อเวลาที่ server ทำงานแล้วเกิดบาง service down เราจะได้ไม่ต้องมานั่ง start stop restart พวก service บ่อยๆ 
วิธีรัน 
1.เข้า Terminal ด้วยสิทธิ์ Root
2.เมื่อสร้างไฟล์เสร็จให้ทำการเปลี่ยนไฟล์ให้ execute ได้ ด้วย
chmod +x filename.py
3. รันด้วยคำสั่ง 
./filename.py 

#!/usr/bin/python
import commands
import time
import os
while True:
 #find process
        pc = commands.getoutput('ps -aux | grep nginx | grep -v grep').strip()

        if(len(pc) > 0):
                print 'process running. skip'
        else:
  print 'process not running'
                #stop program
                os.system('sudo service nginx stop')
                #start program
         os.system('sudo service nginx start')
  print 'process started'
 #Check every 1 minute
        time.sleep(1 * 60)

ไม่มีความคิดเห็น :

แสดงความคิดเห็น