Hello readers, today we will discuss about Crontab jobs.
Q. What is crontabs?
Ans- Cronjob is a service or we can say its a tool that run in backend periodically at fixed times, dates, or intervals.
The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list. crontab stands for "cron table," because it uses the job scheduler cron to execute tasks.
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)
<?php
$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
To delete jobs:
echo exec('crontab -r');
Note: apache is running as a particular user and that's usually not root, which means the cron jobs can only be changed for the apache user unless given crontab -u privilege to the apache user.
0 Comment(s)