I have created a plugin that adds products programatically to WooCommerce. The plugin is working great, but now I need to make a cron job that runs every 5 minutes to update the inventory.
I have the script all written but I need to include calls to get_option() in this php file to get certain plugin values that the user has entered. However, I can't just include get_option() in this file because it is outside of the Wordpress core. So my thought would be to put in require( 'path/to/wp-load.php' ); which I know you aren't really supposed to do. Anyway it fixes the issue if you hit the page via a web browser request. However the cron job fails the moment that this file is included because somewhere with wp-load.php it is sending HTTP_Header requests.
Any thoughts or solutions? I tried to add define('WP_USE_THEMES', false); right above the requiring of wp-load.php but it is still causing the cron job to fail.
Long winded I know, but how do you include get_option() requests inside of a external PHP script that will be accessed via a PHP cron job.
Thanks much.
The quick and easy way
The problem is probably that you try to include wp-load.php from a wrong path. In a CLI environment, the path would not be the same as when you do an HTTP request to the file. So with this you should fixed your issue:
require(dirname(__FILE__) . '/../../../wp-config.php');
The proper but longer way
Based on cale_b comments and this article he linked, there is a much proper way to go by doing a Wordpress Cron job.
First in your plugin add a function that will contain the code needed to be executed, let's call it my_cron_job(). You can eventually just include the script you already wrote in this function. Then add the following to schedule the execution of this every 5min:
// Define a new interval (5 minutes)
add_filter('cron_schedules', 'fively_interval');
function fively_interval($interval) {
$interval['fively'] = array('interval' => 5*60, 'display' => 'Once 5 minutes');
return $interval;
}
// Register the hook on plugin activation
register_activation_hook(__FILE__, 'my_cron_job_activation');
add_action('my_cron_event', 'my_cron_job');
function my_cron_job_activation() {
wp_schedule_event(time(), 'fively', 'my_cron_event');
}
// Unregister the hook on plugin deactivation
register_deactivation_hook( __FILE__, 'my_cron_job_deactivation' );
function my_cron_job_deactivation(){
wp_clear_scheduled_hook( 'my_cron_event' );
}
Then set up your cron to execute wp-cron.php every 5 minutes:
*/5 * * * * php-cli -f [path to your WP]/wp-cron.php
Update
First when choosing the option of executing wp-cron.php with a server cron you should disable the default WP Cron behaviour (execution of cron through web visits):
define('DISABLE_WP_CRON', true);
Secondly, as for your question about WP Cron reliability I see a potential flaw indeed. I'm not 100% sure of that, but I think it is possible that wp_schedule_event get desynchronized with the server cron, as the job get executed only if the interval is past. As it will be re-scheduled depending of the execution time of the script which is slightly different with the server cron time.
For example:
00:00:00:000 Server cron execute wp-cron.php
00:00:00:100 The job can be executed, so let it run
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 5min
00:05:00:000 Server cron execute wp-cron.php
00:05:00:100 The job is planned for 00:05:00:200, no execution !
00:10:00:000 Server cron execute wp-cron.php
00:10:00:100 The job is executed
That's theory of course, maybe this is not accurate. I suggest doing some test and see how it behave. If it indeed behave like I think it did, I suggest as easy workaround to change the wp_schedule_event to a lower interval - 4min for example.
add_filter('cron_schedules', 'fourly_interval');
function fourly_interval($interval) {
$interval['fourly'] = array('interval' => 4*60, 'display' => 'Once 4 minutes');
return $interval;
}
So we'll have the following:
00:00:00:000 Server cron execute wp-cron.php
00:00:00:100 The job can be executed, so let it run
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 4min
00:05:00:000 Server cron execute wp-cron.php
00:05:00:100 The job is planned for 00:04:00:200, so let it run!
00:10:00:000 Server cron execute wp-cron.php
00:00:00:200 Wordpress finished to execute the job - it schedule the event in 4min
00:10:00:100 The job is executed (planned for 00:09:00:200)
With the default WP Cron behaviour disabled it should work flawlessly.
Related
I am using a WordPress site hosted on a DigitalOcean WordPress Litespeed droplet. I am attempting to run a php file in my theme every five minutes using a Cron Job. I don't want to use the default WordPress cron job system.
So far I have connected to the sever through ssh. Then entered the crontab using "crontab -e" then entered the following code then saved / exit
*/5 * * * * php /var/www/html/wp-content/themes/my_theme/cron.php
And inside the cron.php I put a simple mailing function for testing purposes:
<?php
/*template name: Cron*/
$content = date("h:i:sa");
wp_mail( 'reece.r.barrett#gmail.com', 'Cron Test', $content, array());
?>
But I never receive an email so I assume its not working. Is there something wrong with the way I am declaring my cron job? Also I haven't disabled the WordPress Cron system like I have seen a lot of tutorials doing. Is this required to get my own cron to work?
This is how I've seen some tutorials disabling the WordPress cron in wp-config
define('DISABLE_WP_CRON', true);
Your problem is that even if your file physically live inside your theme folder it is actually run "outside" wp core ENV since you are calling it directly. If you ask me, the best, simpliest and cleanest way to use external cron system inside WP ecosystem is to do something like this:
add_action( 'wp_ajax_nopriv_my_cron_action', array( 'myCronAction' ) );
function myCronAction(){
//...your code
}
In that way you make use of WP ajax endpoint to enter the WP ecosystem. Be careful with security since in that way you are exposing a cron url to do your stuff. Securing that is up to you, but that's the easiest way to achieve that.
To use that from your cron you just need to make something that fetches an URL built like this: https://yourdomain.com/wp-admin/admin-ajax.php?action=my_cron_action
If you don't want to make it like that, then you have to bootstrap WP core yourself inside your .php file see https://wordpress.stackexchange.com/questions/47049/what-is-the-correct-way-to-use-wordpress-functions-outside-wordpress-files
First check if your script run from cli/webhook. Then please check for cli execution, since there could be the case that php is not in server's PATH and you have to call full path to php, e.g.:
/usr/local/bin/php /home/your_username/public_html/path/to/cron/script.php
I'm working with Moodle 2.9, and trying to add some new task to cron.
Inside my moodle/theme/portal folder I added a portal_cron() function to lib.php file, then I run cron manually from command line but it's not working ?!
Cron work normally but ignored my new task!
So what I'm doing wrong and how can I add new task to cron ?
Have you got a cron value in moodle/theme/portal/version.php eg:
$plugin->cron = 60; // Every 60 seconds.
Although the cron function is still available, you should use scheduled tasks from Moodle 2.7+
https://docs.moodle.org/dev/Task_API
Solved,
Just changed the function name from portal_cron() to theme_portal_cron() in lib.php, because this file is in moodle/theme/portal/lib.php directory.
I want to run my cron script at sheduled intervels created at blocks/plugin_name/cron.php . Please help me how to create and call this cron script to run at regular intervels.
Cron.php is the old way, you should create a function plugin_name_cron() in blocks/plugin_name/lib.php
Then in version.php you have a cron parameter which will tell Moodle to run the plugin every x seconds.
$plugin->cron = 0; // Seconds.
So every 15 minutes is
$plugin->cron = 15*60; // Seconds.
0 seconds means never run the cron.
This does depend on the admin cron being run too. If the admin cron is run ever 60 minutes then your plugin cron will only be run every 60 minutes.
For testing you can run the admin cron manually from http://yoursite.com/admin/cron.php
If you want to schedule the cron to run at a certain time, then you will need to add some code to your cron function to work out the scheduled time.
EDIT:
Actually the cron function is slightly different for blocks. Create a function cron() in your class class block_plugin_name extends block_base..
UPDATE:
From Moodle 2.7+, the above has been replaced with scheduled tasks https://docs.moodle.org/dev/Task_API#Scheduled_task_usage
I am using the paid host Hosting24 to run my website. I got a cron job which execute the following code every 1 minute.
<?php
require_once('connect.php');
for($c = 0; $c < 60; $c=$c+5)
{
// php to mysql queries SELECT/ UPDATE/ INSERT etc...
sleep(5);
}
mysql_close($my_connection);}
?>
I tried to use the for loop to allow the script to run for 1 minute. Eventually my script should run for as long as I want it to be because the server will execute it every 1 min.
However, I opened my website for a short while and then I cannot connect to it. I cannot even access my cpanel.
Is my cron job script overheating the system, so the system is down?
How should I set up my cron job script to let it run every 1 min and lasts for 1 min?
Thanks.
It's been my experience that cron jobs that need to include files should contain the full path to that file (the CLI environment can differ greatly from the environment inside the web server). Try that and see if it helps.
If not, the next thing you need to do is turn the cron job off and run it from the CLI yourself, using top to look at the process usage. See how long it takes for your cron to run.
First I need to understand how Magento cron works.
I know how cron works on linux, using crontab -e.
I know I need to set up Magento's cron.php to run periodically
But when I define cron within magento config file, how does they match that they should be run?
Because if set my cron.php to run every 15 min (0,15,30,45 * * * *) and I have a Magento cron <schedule><cron_expr>10 * * * *</cron_expr></schedule> for example.
How will it match/work?
By the way, how could I test that my cron works without waiting a long time?
Magento's cron entry point is the cron.php script in your Magento root. You'll need to setup an OS crontab entry hit this script, which you appear to have done already.
How the cron works
Every time the cron.php script is hit three things happen:
Schedule: Magento parses the merged config.xml files for <config><crontab>...</crontab></config> jobs entries, reading their cron_expr elements for detail on how often they should be run. Magento then populates the cron schedule table with jobs that should be executed in the future, along with timestamps for when they should be run. The extent into the future that Magento does this is configurable in the admin.
Execute: Magento reads the cron schedule table for jobs that need to be executed this very second and jobs that should have already been executed, i.e. with timestamps in the past, that haven't expired. The expiry limit is also a parameter configurable in the admin.
Cleanup: Magento goes through the schedule table deleting jobs that have been completed, or missed (due to the deadline).
Duplicate runs?
You raise an interesting point. What happens if you have a 10 minute cron scheduled in Magento, but cron.php is only guaranteed to be hit every 15 minutes. It appears that Magento would run your Magento jobs twice, in some cases:
HH:00 -> HH:50 and HH:00
HH:15 -> HH:10
HH:30 -> HH:20 and HH:30
HH:45 -> HH:40
I've only read the code, and that's what appears to happen. The best way to find out would be, of course, to run it and find out. If you want to avoid the issue, in most cases, then increase the cron.php execution frequency to every 5 minutes, which is what we do.
Testing your code
If you want to test if your cron is actually working without waiting an age, set the cron_expr to execute every minute, or clear out your cron schedule table then hit cron.php twice (once to generate the schedule, and again to run the job).
If you want to just test that your model is working, you could setup a test script (outlined in https://www.nicksays.co.uk/testing-magento-modules), and run that.
Your question prompted a blog post: https://www.nicksays.co.uk/dissecting-the-magento-cron-system :)
On the second half of your question... how to tell if its running.
Here is something that I did. I added the following code at the end of the cron.php file. This updates a simple log called "cronlog.txt" everytime the cron.php file gets called.
So, if I have doubts about cron running I can just glance at that file and see the last date and time the cron.php file ran.
try {
$myFile = "cronlog.txt";
$fh = fopen($myFile, 'w');
$stringData = date('l jS \of F Y h:i:s A');
fwrite($fh, $stringData);
fclose($fh);
} catch (Exception $e) {
Mage::printException($e);
}
adding these to the bottom of cron.php will make it write out cron.php.log on execution with the datetime
try {
Mage::getConfig()->init()->loadEventObservers('crontab');
Mage::app()->addEventArea('crontab');
Mage::dispatchEvent('default');
$log = fopen(__FILE__.'.log', 'a');
fwrite($log, date("Y-m-d H:i:s").PHP_EOL);
fclose($log);
} catch (Exception $e) {
Mage::printException($e);
}
If you're on GNU/Linux you can check /var/log/syslog and filter out the cronjobs by using grep. To see a list of all cronjobs that were run, type in grep 'cron' /var/log/syslog.
To test cron working or not you can simply write Mage::log('cron working', null, 'cron.log'); at the end of the cron.php file.
If the cron is working, it will create a cron.log file in your base dir.