Use PHP to Create, Edit and Delete Crontab Jobs

Use PHP to create, edit and delete crontab jobs?

crontab command usage

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 provided the user has the adequate file write permission.

To delete jobs:

echo exec('crontab -r');

Also, take note that 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.

Edit cron in PHP

you can use some commands to:

  1. export current crontab: crontab -l > /some/file

  2. do your own cooking with editing this file via php

  3. tell cron to use your edited file : crontab /some/file

using exec() to run those commands should be fine.

Ith seems there are also some libraries out there that would do this for you, like Jobby...

Can I modify the schedule of a cron job from a php script that it is being executed by another cron job?

Make sure you do not break something you better monitor your cronjobs after you deploy this But what you want to do is possible.

  1. You can create, modify and delete Cronjobs via PHP (Use PHP to create, edit and delete crontab jobs?)

  2. This is also true if this PHP is executed via another cronjob (as long as you give the correct permissions)

⇒ It is possible (q.e.d.)

Also see this question: https://askubuntu.com/questions/408611/how-to-remove-or-delete-single-cron-job-using-linux-command

This is how you remove a single cronjob (by example) - just create a new one for the different dates:

crontab -u mobman -l | grep -v 'perl /home/mobman/test.pl'  | crontab -u mobman -

set cron job from php

after many search in Dr Google i found out the answer
i have to change the use to Apache user which is www-data
so the command change to crontab -u www-data cron.txt

Use PHP to create, edit and delete crontab jobs?

crontab command usage

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 provided the user has the adequate file write permission.

To delete jobs:

echo exec('crontab -r');

Also, take note that 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.



Related Topics



Leave a reply



Submit