Wrote a Python script to monitor the nginx process

  • 2020-04-02 09:41:17
  • OfStack

 
#!/usr/bin/env python 
import os, sys, time 

while True: 
time.sleep(3) 
try: 
ret = os.popen('ps -C nginx -o pid,cmd').readlines() 
if len(ret) < 2: 
print "nginx process killed, restarting service in 3 seconds." 
time.sleep(3) 
os.system("service nginx restart") 
except: 
print "Error", sys.exc_info()[1] 

Set the executable properties of the file and add /etc/rc.local.
There are other processes that can be monitored in this way, and I'm sure there's monitoring software out there, but I think it's more convenient to write a script.

Related articles: