예제 #1
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;
            }
        }
    }
}
예제 #2
0
    }
}
// *** save use mail template ***
if (isset($_POST['use'])) {
    if (isset($_POST['use'])) {
        $data['mailid'] = $_POST['maillist'];
        if (updateUseMail($data, $db)) {
            header("location: " . ROOT . "admin/mail_create.php");
            exit;
        }
    }
}
if (isset($_POST['mailsetup'])) {
    if (empty($_POST['mailTO'])) {
        header("location: " . ROOT . "admin/mail_setup.php");
        exit;
    }
    $mailTO = empty($_POST['mailTO']) ? "" : implode(":", $_POST['mailTO']);
    $mailCC = empty($_POST['mailCC']) ? "" : implode(":", $_POST['mailCC']);
    $mailBCC = empty($_POST['mailBCC']) ? "" : implode(":", $_POST['mailBCC']);
    $data['mailid'] = trim($_POST['mailid']);
    $data['mailto'] = $mailTO;
    $data['mailcc'] = $mailCC;
    $data['mailbcc'] = $mailBCC;
    if (!updateMail($data, $db)) {
        header("location: " . ROOT . "error.html");
        exit;
    }
}
$getMailList = getMailList($db);
$getMailListAll = getMailListAll($db);