Create a Cron Job in PHP

This is a quick PHP script to create a cron job with PHP, should you have the need to do that. Just replace line 1 with your own cron command. You don’t have to edit anything else. It will first check if the cron exists, then create it only if it does not exist.

(See instead how to delete a cron job with PHP.)


$job = '0 * * * * php /home1/myypath/etc/mycron.php >/dev/null 2>&1';

 * Create a job if it does not exist
function cronjob_exists($command){
    exec('crontab -l', $crontab);
        $crontab = array_flip($crontab);
    return $cronjob_exists;

// create a job if it doesn't exist
if (!cronjob_exists($job)) {
    //add job to crontab
    exec('echo -e "`crontab -l`\n'.$job.'" | crontab -', $output);

See more:

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: