Linux systems use python to monitor apache server process script sharing

  • 2020-04-02 13:19:30
  • OfStack

CRTRL. Py is a Python script that monitors Apache server processes


!/usr/bin/env Python 
import os, sys, time 
while True: 
time.sleep(4) 
try: 
ret = os.popen('ps -C apache -o pid,cmd').readlines() 
if len(ret) < 2: 
print "apache  Process exit exception,  4  Reboot in seconds " 
time.sleep(3) 
os.system("service apache2 restart") 
except: 
print "Error", sys.exc_info()[1]

Set file permissions to execute properties (using the command chmod +x CRTRL. Py) and then add to /etc/rc.local. If the Apache server process exits unexpectedly, the script automatically checks and restarts. Briefly, the script in listing 5 is not based on the /proc pseudo-file system, but on modules provided by Python itself. The embedded time template in Python is used here, and the time module provides functions for various operation times.


Related articles: