从PHP脚本调整crontab
我在HTML中有一组[开/关]按钮,我使用PHP对按下的按钮做出反应。[On]按钮应该在crontab中添加一行。[Off]按钮应删除特定crontab用户的所有条目 问题: 无法从index.php编辑crontab 什么有效: 如果直接从终端发送“添加”和“删除”脚本,则它们可以工作。两个按钮都有反应,可以从每个if语句中打印出第一条回显行 问题澄清: 为什么index.php不能触发其他php脚本 Index.php从PHP脚本调整crontab,php,cron,Php,Cron,我在HTML中有一组[开/关]按钮,我使用PHP对按下的按钮做出反应。[On]按钮应该在crontab中添加一行。[Off]按钮应删除特定crontab用户的所有条目 问题: 无法从index.php编辑crontab 什么有效: 如果直接从终端发送“添加”和“删除”脚本,则它们可以工作。两个按钮都有反应,可以从每个if语句中打印出第一条回显行 问题澄清: 为什么index.php不能触发其他php脚本 Index.php <!-- Form --> <form method
<!-- Form -->
<form method="post">
<input id="button_on" type="submit" name="button_on" value="on">
<input id="button_off" type="submit" name="button_off" value="off">
</form>
<?php
/**
* Button logics.
*/
// Coalesce operator. If input is none, add 0.
$button_on = $_POST["button_on"] ?? 0;
$button_off = $_POST["button_off"] ?? 0;
if(!$button_on == 0) {
echo "Button [on] pressed!"; // Verify react on button.
$output2 = shell_exec('php 1_add_crontab_lines.php');
echo $output2;
}
if(!$button_off == 0) {
echo "Button [off] pressed!"; // Verify react on button.
$output3 = shell_exec('php 2_delete_crontab_lines.php');
echo $output3;
}
?>
<!-- For troubleshooting. -->
<pre>
---------------------------------
Content of [$_POST]
---------------------------------
<?php print_r($_POST) ?>
<?php
echo exec('crontab -r');
---------------------------------
[$\u POST]的内容
---------------------------------
1\u add\u crontab\u lines.php
<!-- Form -->
<form method="post">
<input id="button_on" type="submit" name="button_on" value="on">
<input id="button_off" type="submit" name="button_off" value="off">
</form>
<?php
/**
* Button logics.
*/
// Coalesce operator. If input is none, add 0.
$button_on = $_POST["button_on"] ?? 0;
$button_off = $_POST["button_off"] ?? 0;
if(!$button_on == 0) {
echo "Button [on] pressed!"; // Verify react on button.
$output2 = shell_exec('php 1_add_crontab_lines.php');
echo $output2;
}
if(!$button_off == 0) {
echo "Button [off] pressed!"; // Verify react on button.
$output3 = shell_exec('php 2_delete_crontab_lines.php');
echo $output3;
}
?>
<!-- For troubleshooting. -->
<pre>
---------------------------------
Content of [$_POST]
---------------------------------
<?php print_r($_POST) ?>
<?php
echo exec('crontab -r');
很可能是权限问题。如果通过命令行运行PHP脚本,它将使用您的用户。如果通过web服务器运行PHP脚本,则执行脚本的将是web服务器用户。通常,web服务器用户没有足够的权限来编辑系统(这是一件好事)。@MagnusEriksson我认为你的理论得到了加强,因为“添加/删除”php脚本可以从终端运行,但从浏览器直接运行相同的脚本(没有index.php)不起作用。这很可能是权限问题。如果通过命令行运行PHP脚本,它将使用您的用户。如果通过web服务器运行PHP脚本,则执行脚本的将是web服务器用户。通常,web服务器用户没有足够的权限来编辑系统(这是一件好事)。@MagnusEriksson我认为你的理论得到了加强,因为“添加/删除”php脚本可以从终端运行,但直接从浏览器运行相同的脚本(没有index.php)是不起作用的。