';
renderJobTable($systemJobs, 'system');
echo '';
exit;
}
// 处理登录
if (isset($_POST['login'])) {
if ($_POST['password'] === $valid_password) {
$_SESSION['authenticated'] = true;
} else {
$error = "密码错误,请重新输入!";
}
}
// 检查认证状态
if (!isset($_SESSION['authenticated']) || !$_SESSION['authenticated']) {
showLoginForm();
exit;
}
// 处理Cron操作
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
handleCronOperations();
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
showManagementInterface();
// 功能函数
function showLoginForm() {
global $error;
?>
Cron管理登录
$error
"; ?>
/dev/null 2>&1";
// 直接写入系统
exec("(crontab -l; echo " . escapeshellarg($cronLine) . ") | crontab -", $output, $result);
// 记录到存储文件
$existing = file_exists($storageFile) ? file($storageFile, FILE_IGNORE_NEW_LINES) : [];
if (!in_array($cronLine, $existing)) {
file_put_contents($storageFile, $cronLine . PHP_EOL, FILE_APPEND);
}
}
function addFullCronJob($command) {
global $storageFile;
$command = trim($command);
// 直接写入系统
exec("(crontab -l; echo " . escapeshellarg($command) . ") | crontab -", $output, $result);
// 记录到存储文件
$existing = file_exists($storageFile) ? file($storageFile, FILE_IGNORE_NEW_LINES) : [];
if (!in_array($command, $existing)) {
file_put_contents($storageFile, $command . PHP_EOL, FILE_APPEND);
}
}
function deleteCronJob($cronLine) {
exec("crontab -l | grep -v -F " . escapeshellarg($cronLine) . " | crontab -", $output, $result);
return $result === 0;
}
function deletePendingJob($cronLine) {
global $storageFile;
$existing = file_exists($storageFile) ? file($storageFile, FILE_IGNORE_NEW_LINES) : [];
$existing = array_filter($existing, function($line) use ($cronLine) {
return trim($line) !== trim($cronLine);
});
file_put_contents($storageFile, implode(PHP_EOL, $existing) . PHP_EOL);
}
function getCronJobs() {
exec("crontab -l", $output);
return array_filter($output, function($line) {
return trim($line) !== '' && !str_starts_with($line, '#');
});
}
function getPendingJobs() {
global $storageFile;
if (!file_exists($storageFile)) return [];
return file($storageFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
function validateCronCommand($command) {
$pattern = '/^((\*(\/\d+)?|\d+([\-\/]\d+)*(,\d+([\-\/]\d+)*)*)\s+){5}(.*)$/';
return preg_match($pattern, trim($command));
}
function renderJobTable($jobs, $type) {
if (!empty($jobs)) {
echo '';
foreach ($jobs as $job) {
$encodedJob = urlencode($job);
echo "
| ".htmlspecialchars($job)." |
|
";
}
echo '
';
} else {
echo ''.($type === 'system' ? '系统当前没有定时任务' : '持久化列表中没有挂起任务').'
';
}
}
function showManagementInterface() {
global $error, $storageFile;
$systemJobs = getCronJobs();
$pendingJobs = getPendingJobs();
$defaultCommand = '* * * * * /usr/home/yourname/domains/yourdomains/public_html/restart.sh';
?>
Cron任务管理
$error
"; ?>
Cron任务管理