Is it possible to setup a CRON JOB Programmatically? Using PHP with the php_curl
extension? I have the following code:
function wp_cron_control_call_cron( $blog_address ) {
$cron_url = $blog_address . '/wp-cron.php?doing_wp_cron';
$ch = curl_init( $cron_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 0 );
curl_setopt( $ch, CURLOPT_TIMEOUT, '3' );
$result = curl_exec( $ch );
curl_close( $ch );
return $result;
}
Is there anyway to execute this on a set interval programmatically? I’m not trying to setup a CRONTAB or anything like that in cPanel manually. I would like this to be able to be done with actual code, if possible. Is this possible? Is there a setting in curl_setopt
to do this with? Or some other way? Using PHP 5.4.16 if that matters.
Why not using cPanel API v2 ?
cPanel Inc created a Client XML API, guess what.. it’s using cURL to call API.
Firstly get the xmlapi.php file, now search in the xmlapi.php these lines :
In order to make it work with a cPanel account without root access, change the
$port
to 2083 (HTTPS) or 2082 (HTTP), obviously, if you’re using HTTP-Port, change the$protocol
to http.cPanel API has a Cron module documentation as you asked, you can delete, add and even edit a CronJob.
Example of use
List all CronJob :
Per example, with a single line in CronJob it’ll return :
Create a CronJob
Obviously, it returns you this :
Removing a CronJob
Before explaining how to remove a CronJob, you have to know the line of the CronJob that you want to remove.. If you check “List all CronJob” on the response part, you may saw a count in the JSON response, exactly this :
The exact line is the
"count":1
after the"hour":1
and NOT the latest"count":2
, as you understand, the line is the….. FIRST (well done sherlock).Now we can use the same script with a Curl::remove_line :
And output :
Editing a CronJob
AGAIN You need the
linekey
OR the line number (calledcommandnumber
) in order to edit a line, the code is exactly the same except that there is a linekey and line params , check the Cron::listcron response for linekey, per example here :The
linekey
param is 7209fe24c876a729b42a929692c62ce3 and thecommandnumber
is 1 (see count here :"hour":"1","count":1
)There is the code :
PS : If you use linekey you can leave commandline empty and vice-versa.
I make it simple.. but there is a ton of possibilities using POST request etc..
libssh2 way
I found a way to do it with libssh2, checkout here
shell_exec way
Only disadvantage is that shared hosting or even a correct webmaster is not going to enable shell function but.. if there’re enabled, you should check here a solution was given by @ajreal, but it also depends on what user crontab..
Hope this helped you!
How to enable CURL extension
wordpress WP-Cron-Control
Managing Cron Jobs with PHP
Execute a cron job every 30 Minutes
Note: In the same way, use */10 for every 10 minutes, */15 for every 15 minutes, */30 for every 30 minutes, etc.
Full Source (Save it to any PHP File and Add To Plugin Folder and Activate)
Function
You can add following php code :
You can modify $mycronjob value as you like.
To setup a ‘cron job’ on your system, you modify the crontab file by adding a new line in the specific format.
From Wikipedia
So to execute your job every 30 minutes
To make this string into a cron job it needs to be added to the crontab file, there are quite a few different ways people have done this via scripting. Once you find one you like, you can then use PHP to execute the script.
To do this via PHP you can leverage the shell_exec() function
Try something like this:
Remember that $srtCron is:
Some reference:
Add Jobs To cron Under Linux or UNIX
Appending to crontab with a shell script
Editing crontab programmatically from PHP is hard and painful. My advice for you is simple.
Now you want to edit a cron ? Just edit your Mysql table (by hand or from a php script). It should solve all you problems.
This is just an example
you can visit How can I programmatically create a new cron job? to know commands much better way,
and to understand the corn you can go to
Running a simple shell script as a cronjob
PHP to create Cronjob
PHP to delete Cronjob
This will delete the whole crontab file.
You may try this:
Doesnât WordPress already have built in pseudo-cron capabilities? The function
wp_schedule_event()
seems to handle what you are describing right within the WordPress API:Here is an example of how to setup an hourly event that includes
wp_next_scheduled()
:So using your example code, this should work:
Also, you could do something similar in plain PHP by simply storing the timestamp somewhere. Either in a DB field, or perhaps as in a file stored in a cache directory or tmp directory. Then just check that file each time a page loadsâfor exampleâand if the time the page loads has a delta outside of the timespan you have checked, run the task.
Or instead of a PHP page load you could actually have a real cron job run every minuteâfor exampleâthat would load a PHP page that then does the delta comparison logic based on your needs.