示例#1
0
文件: job.php 项目: ZerGabriel/cms-1
 public function set_next_run()
 {
     if (empty($this->interval) and empty($this->crontime)) {
         return;
     }
     if (!empty($this->crontime)) {
         $this->date_next_run = date('Y-m-d H:i:s', Crontab::parse($this->crontime));
     } else {
         if (!empty($this->interval)) {
             $this->date_next_run = date('Y-m-d H:i:s', time() + $this->interval);
         }
     }
     return $this;
 }
示例#2
0
function cronRun($cronPath, $eth = 'eth1', $errFilePath = '/tmp/cron.err')
{
    $crontab = file($cronPath);
    $now = time();
    delOldLog($now);
    $mailArr = array();
    $newErrFile = $errFilePath . '.new';
    foreach ($crontab as $cron) {
        $cron = ltrim($cron);
        if ($cron[0] === '#') {
            continue;
        }
        $slices = preg_split("/[\\s]+/", $cron, 6);
        if (count($slices) !== 6) {
            continue;
        }
        $cmd = trim(array_pop($slices));
        $mailArr = getMailList($cmd);
        $cron_time = implode(' ', $slices);
        $next_time = Crontab::parse($cron_time, $now);
        if ($next_time !== $now) {
            continue;
        }
        $pid = pcntl_fork();
        if ($pid == -1) {
            die('could not fork');
        } else {
            if ($pid) {
                // we are the parent
                pcntl_wait($status, WNOHANG);
                //Protect against Zombie children
            } else {
                // we are the child
                // 			`$cmd`;
                $stdout = $stderr = $result = null;
                $result = Sexec($cmd, $stdout, $stderr, 360000);
                $stdout = rtrim($stdout);
                $stderr = rtrim($stderr);
                $exec_time = time() - $now;
                $now = date('Y-m-d H:i:s', $now);
                $suff = date('Y-m-d');
                if (($fp = @fopen('/tmp/cron.log.' . $suff, 'a')) === FALSE) {
                    echo 'error open cron log file';
                    exit(1);
                }
                @flock($fp, LOCK_EX);
                @fwrite($fp, "{$now} cmd:'{$cmd}' result code:{$result} Exec time:{$exec_time}\nstdout:{$stdout}\nstderr:{$stderr}\n");
                @flock($fp, LOCK_UN);
                @fclose($fp);
                if ($result != '0') {
                    writeTempError("{$now} cmd:'{$cmd}' result code:{$result} Exec time:{$exec_time}\nstdout:{$stdout}\nstderr:{$stderr}\n", $errFilePath);
                    foreach ($mailArr as $mail) {
                        $ip = trim(getIP($eth));
                        shell_exec('cat ' . $errFilePath . ' | sed \'s/\\r//\' >' . $newErrFile);
                        shell_exec('export LANG=en_US.UTF-8;mail -s "crontab exec Error at ' . $ip . '" ' . $mail . '<' . $newErrFile);
                    }
                }
                exit;
            }
        }
    }
}