crontab命令用法
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
所以,
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
以上可以用于创建和编辑/附加,只要用户具有足够的文件写入权限。
删除作业:
echo exec('crontab -r');
另外,请注意,apache作为特定用户运行,通常不是root,这意味着cron作业只能为apache用户更改,除非为apache用户提供crontab -u权限。