我想在网站上添加设置cron作业的功能。经过大量的研究,我发现从理论上讲我可以将每个作业保存在自己的文件中,然后使用以下命令:crontab /path/to/file
将其保存到crontab中。我的问题是将更改保存到crontab中,因为如果触发我的PHP脚本后,我将在终端中手动键入此命令,则一切正常。如果您知道通过php设置cron作业的其他方法,我将为您开放。
我当前的php代码:
$myfile = fopen("Crontab/cronJobs.txt", "a+") or die("Unable to open file!");
$m = $_POST['m'];
$h = $_POST['h'];
$d = $_POST['d'];
$mon = $_POST['mon'];
$wd = $_POST['wd'];
$p = $_POST['p'];
$s = $_POST['s'];
if(!isset($m)) $m="*";
if(!isset($h)) $h="*";
if(!isset($d)) $d="*";
if($mon==-1) $mon="*";
if($wd==-1) $wd="*";
$txt = "$m $h $d $mon $wd gpio write $p $s\n";
fwrite($myfile, $txt);
fclose($myfile);
shell_exec("crontab /var/www/html/Crontab/cronJobs.txt");
header( "Location: main.php");
编辑1:当我查看www数据用户Crontab(从中运行服务器的用户)时,它已更改,并且无法正常工作;在尝试再次执行此操作后,它没有更改。我尝试手动更新crontab,但它也不起作用。我也尝试过使用root crontab,但是它不能通过网站更改,并且当我手动更新它时不起作用。