How to setup cron script in moodle through plugin - php

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

Related

Cron Job for laravel web application

So I have this laravel web application installed on a Dreamhost server. At the admin end I was given this line of code at the beginning
Please Set Cron Job Now
To automate the return interest, we need to set the cron job and make sure the cron job is running properly. Set the Cron time as minimum as possible. Once per 5-15 minutes is ideal while once every minute is the best option.
curl -s https://capitalmaxoptions.com/cron
How do I go about this? Does it go into the kernel.php file?
Yes, you can have the job in kernel.php file.
Best option is to make a command for your cron job so you don't mix the specific job with the others. You can check about Making Commands.
Inside your handle function you may have your execution
public function handle()
{
// cURL request or your backend function to your logic
}
Next is to schedule the frequency of the command which (5-15 minutes) in you case. You can check about the Frequency Options.In your kernel.php file, inside the schedule function you can call your function and set the frequency.
protected function schedule(Schedule $schedule)
{
// If your command is cron:interest
$schedule->command('cron:interest')->->everyFifteenMinutes();
}
You can test your command by starting the scheduler manually on localhost. Check the Run Task Scheduler link shared in the comments #Autista_z shared to configure scheduler.

Add custom task to Moodle cron job

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.

Wordpress get_option in External PHP file

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.

Cron job runs 2 times and then stops

I m trying to configure a cron job via cpanel. What i want to do is execute a php script that updates a value in a mysql database table every minute. i tryed 2 cron commands :
wget -O – -q "URL_HERE"
and
usr/local/bin/php -q /home/user/public_html/filename.php
note that its a very small script with tiny execution time, so it should be ok.
for testing purposes I configured it to run every minute to see if it works.
Here is the problem:
the cron job runs ok for the first two times , but then it stops. for example if the time is 12:00, the script will run on 12:01, then on 12:02, but NOT on 12:03 .. 12:04 and so on. What could be the problem here?
thanks in advance
Make sure your crontab entry is correct...
For running a cron every minute, one needs to add entry something like
* * * * * "username" "task to execute"

cron jobs bring down the server?

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.

Categories