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
特权。