如何使www数据(Apache)访问ubuntu中根的Crontab


How to make www-data (Apache) to access Crontab of root in ubuntu?

我想从网页访问根crontab。我是怎么做到的。我有一个项目,我必须在网页上列出所有的工作。之后,如果需要,我们需要编辑配置的cronjobs的时间。

请帮我做这个。

使用visudo并添加

`APACHEUSER  ALL=NOPASSWD: /usr/bin/crontab`

APACHEUSER是apache像www data或apache一样运行的用户/usr/bin/crontab是指向crontab的路径,您可以尝试which crontab来查找路径

<?php
$result = shell_exec('sudo -u root -S crontab -l 2>&1'); // 2>&1 allows you to get stderror
echo $result;
file_put_contents('/tmp/crontab.txt', $result.'* * * * * /path_to_cron'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?