Example #1
0
/**
 * array(
 *  pid: 进程id
 *  time: 运行时间
 *  sleep:休眠时间
 * )
 */
function yze_daemon_status()
{
    $pid = yze_getpid();
    if (!$pid) {
        return array();
    }
    $f = popen("ps -eo pid,tty,user,comm,etime | grep '{$pid} .* php'", "r");
    $string = fgets($f);
    pclose($f);
    if (!$string) {
        return array();
    }
    $cliinfo = preg_match_all("/[^\\s]+/", trim($string), $matchs);
    $sleep = function_exists("yze_get_sleep") ? yze_get_sleep() : 60;
    return array('pid' => $pid, "time" => trim(@$matchs[0][4]), "sleep" => $sleep);
}
Example #2
0
File: cli.php Project: ydhl/yangzie
function yze_run_daemon()
{
    yze_daemon_log("yze_run_daemon");
    if (yze_daemon_status()) {
        yze_daemon_log("can not start, YZE Daemon is running");
        exit;
    }
    $pid = pcntl_fork();
    if ($pid == -1) {
        yze_daemon_log("could not fork YZE Daemon, abort.");
        exit;
    } else {
        if ($pid) {
            // we are the parent
            yze_savepid($pid);
            exit;
        }
    }
    //here is child
    // detatch from the controlling terminal
    if (posix_setsid() == -1) {
        yze_daemon_log("could not detach from terminal. abort. ");
        exit;
    }
    // setup signal handlers
    pcntl_signal(SIGTERM, "sig_handler");
    pcntl_signal(SIGHUP, "sig_handler");
    // loop forever performing tasks
    while (1) {
        yze_daemon_log("yze_run_daemon " . date("Y-m-d H:i:s"));
        foreach ((array) yze_get_jobs() as $job) {
            if (function_exists($job)) {
                yze_daemon_log("call  {$job} at " . date("Y-m-d H:i:s"));
                $callinfo = call_user_func($job);
                yze_daemon_log("called:  {$callinfo}");
            }
        }
        sleep(function_exists("yze_get_sleep") ? yze_get_sleep() : 60);
    }
}