среда, 11 ноября 2015 г.

How to configure monit (a daemon monitoring tool) for debian

Monit is a very simple tool for monitoring daemons or processes in linux.

#First of all, install the monit using apt-get:
user@yourhost:~# apt-get install monit

#Edit the configuration file:
user@yourhost:~# nano /etc/monit/monitrc

#####
#add to the end of file the following:


set httpd port 2812 and
    use address localhost
    allow localhost

check process test_daemon with pidfile /var/test_daemon.pid
start program = "/etc/init.d/test_daemon start" with timeout 60 seconds
stop program = "/etc/init.d/test_daemon stop"


#to check the monit status, type:
user@yourhost:~# monit status 

The Monit daemon 5.4 uptime: 4m

Process 'test_daemon'
  status                            Running
  monitoring status                 Monitored
  pid                               31233
  parent pid                        1
  uptime                            6m
  children                          0
  memory kilobytes                  6700
  memory kilobytes total            6700
  memory percent                    0.6%
  memory percent total              0.6%
  cpu percent                       0.0%
  cpu percent total                 0.0%
  data collected                    Thu, 12 Nov 2015 06:59:23

System 'system_yourhost'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.00] [0.01] [0.05]
  cpu                               0.5%us 0.2%sy 0.1%wa
  memory usage                      311036 kB [2.2%]
  swap usage                        0 kB [0.0%]

  data collected                    Thu, 12 Nov 2015 06:59:23


Monit is configured. Daemon is monitored. That's all!

пятница, 23 октября 2015 г.

How to write a PHP daemon for linux debian wheezy

This article will show how to create a simple, fast and efficient php daemon for linux. It's done thanks to the easy_php_daemon php calss that I have wrote myself.

Easy steps to create the daemon:

#Firstly, copy the template file for managing your daemon
user@yourhost:~# cp /etc/init.d/skeleton /etc/init.d/your_daemon

#Make your daemon exacutable
user@yourhost:~# chmod +x /etc/init.d/your_daemon
user@yourhost:~# chmod +x /your_daemon_path

#Edit the following linies 
user@yourhost:~# nano /etc/init.d/your_daemon
##################################################
DESC="your description"
NAME="your daemon name" # it shall be less then 15 characters.
DAEMON="your php daemon path" #example: /home/your_daemon.php
PIDFILE="path to your pid file" #file that will contain your process id.

#change the verbose settings /lib/init/vars.sh
user@yourhost:~# nano /lib/init/vars.sh
VERBOSE=yes

#Add the daemon to autostart
user@yourhost:~# update-rc.d your_daemon default

#Download easy_php_daemon
user@yourhost:~# git clone https://github.com/indigo7333/easy_php_daemon.git

#create your daemon file
user@yourhost:~# nano /your_daemon.php

#!/usr/bin/php -q
<?php
require_once("easy_php_daemon/indigo_daemon.class.php");

class yourclass extends indigo_daemon
{
  public function start_process()
  {
      //your daemon work
      return 1;
  }  
  public function include_libraries()
  {
      //include some libraries
  }
}

  
$daemon=new yourclass();
$daemon->log_file='/path/to/log_file';  //Path to Log file
$daemon->pid_file='/path/to/pid_file';    //Path to PidFile
$daemon->daemon_name='Your Daemon Name';  //Your Daemon Name
$daemon->init();
$daemon->write_log_file("test"); //write something to log file
$daemon->include_libraries();
$daemon->infinite(1000000); //execute the process every second
$daemon->shutdown();

That's all to create a simple and easy php daemon for debian wheezy linux.