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:
export current crontab:
crontab -l > /some/file
do your own cooking with editing this file via php
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.
You can create, modify and delete Cronjobs via PHP (Use PHP to create, edit and delete crontab jobs?)
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
How to Sum All Column Values in Multi-Dimensional Array
How to Add PHP Code/File to Html(.Html) Files
Best Way to Use PHP to Encrypt and Decrypt Passwords
Stacking Multiple Ternary Operators in PHP
How to Get the Last Inserted Id of a MySQL Table in PHP
What Is the Use of the @ Symbol in PHP
How to Store My Users' Passwords Safely
Using Jquery $.Ajax to Call a PHP Function
Single Result from Database Using MySQLi
How to Check If an Email Address Exists Without Sending an Email
Apache Shows PHP Code Instead of Executing It
What Is the Most Accurate Way to Retrieve a User'S Correct Ip Address in PHP
Asynchronous Shell Exec in PHP