May 20 2008

Monitoring a daemon

Category: LinuxBipin Balakrishnan @ 10:42 pm

monit-logo.gif

Well we have created a PHP daemon.One major issue with all daemon is that some time it got killed with out any reason.Here we have a solution for monitoring the daemon with a utility called monit.monit is a utility for managing and monitoring, processes, files, directories and devices on a Unix system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. E.g. monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses to much resources. You may use monit to monitor files, directories and devices for changes, such as timestamp changes, checksum changes or size changes.
For doing it install the utility first

sudo apt-get install monit

We have to edit some configuration file to make the monit work.
Edit sudo vim /etc/default/monit

Set startup=1 from 0

Edit sudo vim /etc/monit/monitrc
This is the file where we specify what to monit
Uncomment

set daemon  60 #will check the daemon every 1 minute
set httpd port 2812 and
 use address localhost  # only accept connection from localhost
 allow localhost        # allow localhost to connect to the server and
#specifying the program for monitor
check process php with pidfile "/var/run/process.pid"
start = "/usr/bin/php /var/www/voicealerts/process.php"
stop = "/usr/bin/php /var/www/voicealerts/process.php"

Line 1 starts a process service entry. It contains the keyword check and a descriptive name for he process to monitor, in this case php. Monit also require the process pidfile to be stated.
If you want monit to start, stop or restart the process you must submit a start and stop program.
Line 2-3 defines those programs for this process
Write and quite the file and check the syntax is ok with command

sudo monit -t

start monit deamon by

sudo /etc/init.d/monit start

start the files specified in the monitrc by

sudo monit start all

check the status of our daemon

sudo monit status

This is a sample configuration monitors the PHP daemon process mentioned in the above post.

Tags: ,


May 20 2008

Making PHP program as Daemon

Category: PHPBipin Balakrishnan @ 10:05 pm

It is some time necessary to make a PHP progam as a daemon .Here it explains how it is possible. Process Control support in PHP implements the Unix style of process creation, program execution, signal handling and process termination. Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment.

The process control functions come along with the php-cli in linux.So we don’t need to install extra libraries for this to work.If php-cli is not there in your machine intall it with yum or apt-get.As i am using the ubuntu os im explaining with its command Ubuntu users can follow this.

bipin@bipin-laptop:~$ sudo apt-get install php5-cli

Copy and paste the code for background process

<?php
include_once('createdb.php');
declare(ticks=1);
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
exit(); // we are the parent
} else {
// we are the child
}
// detatch from the controlling terminal
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
$posid=posix_getpid();
$fp = fopen("/var/run/process.pid", "w");
fwrite($fp, $posid);
fclose($fp);
// setup signal handlers
 pcntl_signal(SIGTERM, "sig_handler");
 pcntl_signal(SIGHUP, "sig_handler");
// loop forever performing tasks
 $dbobject = new DB();
 $dbobject->getCon();
 while (1) {
// do something interesting here, here i have called a function from other flile called "createdb.php"
$dbobject->CopyCallFiles();
}
 fclose($fp);
 function sig_handler($signo)
 {
switch ($signo) {
 case SIGTERM:
 // handle shutdown tasks
 exit;
 break;
 case SIGHUP:
 // handle restart tasks
 break;
 default:
 // handle all other signals
 }
}
?>

Run the program and the function $dbobject->CopyCallFiles(); will work as a deamon here we can include a file instead of the function also here.

Tags: , ,