Recent Posts

Showing posts with label crontab. Show all posts
Showing posts with label crontab. Show all posts

PHP: Create, edit and delete crontab jobs

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)
So

<?
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
?>
The above can be used for both create and edit/append

for delete

echo exec('crontab -r');
Also, take note that apache running on certain user and usually is not root, which mean the cron can only change for apache user unless given crontab -u privilege to apache user.