【发布时间】:2022-01-06 20:57:55
【问题描述】:
我想每天编辑 cron 选项卡以删除/添加一些作业 .. 所以我添加了 cron 作业(助手作业)来运行 php 脚本来处理这些编辑。
当我通过浏览器运行此脚本时工作正常.. 但是当它使用未运行的辅助作业运行时,我从 cpanel 收到带有此错误的通知邮件:
请注意,我使用 C-Panel 的共享主机计划,所以我没有 root 访问权限.. 但是从浏览器运行时代码运行良好。
错误:
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
PHP 脚本:
exec('crontab -l', $crontab);
$record_found = false;
foreach($crontab as $key => $value){
if(strpos($value, 'record_extra.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
$record_found = true;
}
if(strpos($value, 'record.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
}
}
if($record_found){
file_put_contents('/tmp/crontab.txt', arrayToString($crontab) . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
} else {
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
}
需要你的帮助。 提前致谢。
【问题讨论】:
-
所以很可能是权限问题。为什么你从脚本运行 crontab 而不是只编辑
/etc/cron.daily文件? -
这似乎是一个类似 SELinux 或 AppArmor 的安全程序。您需要找到您的系统正在使用哪一个并授予用户 dcfn35c8st1b 适当的权限。
-
请注意我与 C-Panel 共享托管计划,所以我没有 root 访问权限.. 但是从浏览器运行时代码工作正常
-
即使你的 web 服务器 PHP 进程在与命令行 PHP 相同的用户下运行(我不知道是不是这样),权限也可以设置不同。