This article describes how to use python to monitor apache server processes in linux. for details, refer to crtrl. py to monitor the Python scripts of Apache server processes.
The code is as follows:
! /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 "The apache process exits unexpectedly and restarts in 4 seconds"
Time. sleep (3)
OS. system ("service apache2 restart ")
Except t:
Print "Error", sys. exc_info () [1]
Set the file permission to the execution attribute (use the command chmod + x crtrl. py), and then add it to/etc/rc. local. once the Apache server process exits unexpectedly, the script is automatically checked and restarted. The script in listing 5 is not based on the/proc pseudo file system and is implemented based on some modules provided by Python. The embedded time template of Python is used here. the time module provides various time functions.