Jan 042013
 
Article Apache

A typical internet web server, either a WordPress blog, or a site running Joomla, Drupal or other kind of Content Management System, requires at least two services that must be continuously running; an apache web server, and a MySQL DBMS.

But even the in most stable server, it may happen that one of these services meets an unexpected condition that forces it to stop working.

For this reason, it is very desirable to have a “service monitor” script to check periodically the status of these critical services, and perform the required actions when it finds that one of them is not running, such as sending an alert by email, and trying to start it again.

In this post a simple script is presented that monitors the status of the apache and mysql services on a linux server. The sample script can easily be extended to monitor other critical services as well.

Process identifiers

In linux, every process has a numerical process identifier (process ID, PID), assigned by the system when it starts executing. In linux there are also a good number of command line tools to interact with a process, giving them the the process ID as an argument.

To retrieve the ID of a process, we can use commands such as “ps” (process status) and “top”. For instance, to get the PIDs of the apache processes running in a Debian server:

$ ps -fC apache2

In this example, there are five running apache processes. The process with UID “root” and PID “8065” is the parent process. The value of the PPID (Parent PID) of the other four processes is also “8065”, meaning they are children processes of this parent process.

Apache PID file

On the other hand, in the configuration file “/etc/apache2/envvars” of the apache web server, it is possible to specify the name of a file where the parent process writes its PID when it starts executing. In a typical installation, this PID file is “/var/run/apache2.pid”:

MySQL PID file

MySQL also writes to a file the PID of the main process. the name of this file can be specified as an argument “–pid-file”, and can be retrieved from the output of the “sudo mysqld –print-defaults” command:

As we can see, the name of the MySQL PID file in this example is “/var/run/mysqld/mysqld.pid”.

Check if a process is running

Once we know the PID, we can get a good amount of information about the process with commands such as  ps, lsof, strace, etc.

For our purpose of writing a service monitor, we just want to state if the apache and mysql services are running. This can be done running “ps” with the process ID as an argument. “ps” will return a non-zero status code if there is no running process with that PID.

In that case, the script will perform recovery actions. The sample “service_monitor.sh” script below monitors the status of the apache web server, and if it finds that the service is not running, prints an error message and tries to start again the web server

The script can be easily extended to check the status of the mysql process as well:

Automating the service monitor

Once the script is ready to monitor the critical services, it can be scheduled to run every five minutes with an entry in the crontab:

As we have seen, the script prints a message if it finds that a service is not running. This output is sent by the cron daemon in an email to the user for which the crontab entry has been defined.

 Posted by at 11:42 am

 Leave a Reply

(required)

(required)