Using python to monitor apache server process scripts in linux

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.