PHP Classes

File: daemon.php

Recommend this page to a friend!
  Classes of Tim de Koning  >  PHP Cron Daemon  >  daemon.php  >  Download  
File: daemon.php
Role: Application script
Content type: text/plain
Description: Full package file
Class: PHP Cron Daemon
Database driven PHP job scheduler like cron
Author: By
Last change:
Date: 8 years ago
Size: 2,240 bytes


Class file image Download
 * This file is released under the GPL, any version you like
 * PHP CronDaemon v1.0
 * By Robin Speekenbrink
 * Kingsquare Information Services, 1-10-2007
 * Based on the original PHP_FORK class by Luca Mariano
 * This program is free software. You can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 * @author Robin Speekenbrink <>
 * @version 1.0
 * I juse ADODB classes from but for clearity reasons i use the db_functions from this file.
 * Changing to another DB shouldn't pose any problems.

require './lib/miniDb.php';

$db = null;
$fp = $debug = false;

define ('MAX_WORKERS', 15);
define ('MAX_IDLE', 180);
define ('SLEEP_INTERVAL', 10);
define ('FS_START', dirname(__FILE__).'/');
define ('FS_LOG', FS_START.'log/cron.log');
define ('FS_KILL', FS_START.'kill/daemon');
define ('DB_HOST', 'localhost');
define ('DB_USER', 'cronUser');
define ('DB_PASS', 'example');
define ('DB_NAME', 'crontab');
define ('TABLE_CRONTAB', 'crontab');
define ('TABLE_CRONJOB', 'cronJob');

 * A central function for each sub process to create a new instance of the DB handle.
 * My version uses ADODB
 * @return miniDb
function newDb() {
$db = & new mysqldb();
$db->pconnect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

 * Write the data to the specified log file
 * @param string $data
function writeLog($data = '') {
    if (empty(
$data)) return;
    if (!
$fp) $fp = fopen(FS_LOG, 'a+');
$data = date('d-m-Y H:i:s')."\r\n".$data;
fputs($fp, $data, strlen($data));
fputs($fp, "\r\n----\r\n");
    if (
$debug) print $data;
$controller = new cronDaemon('cronDaemon', SLEEP_INTERVAL, MAX_IDLE);
writeLog('starting deamon: '.$controller->getName().' ('.$controller->getPid().')');