How to: Running specific cron tasks via command line

How to: Running specific cron tasks via command line

by Jerry Lau -
Number of replies: 9

Hello there.

Moodle version: 3.2.4+, PHP 5.6.x, MySQL 5.6, RHEL 64-bit

We have a test platform and would not like the cron to execute sending out email notifications to users and wanted to run these specific ones.

  • \core\task\badges_cron_task
  • \core\task\completion_daily_task
  • \core\task\completion_regular_task

When I tried to run one of them, I get the following error:

[root@testmoodle testmoodle]# php lib/classes/task/badges_cron_task.php -v
PHP Fatal error:  Class 'core\task\scheduled_task' not found in /opt/rh/httpd24/root/var/www/html/testmoodle/lib/classes/task/badges_cron_task.php on line 29
PHP Stack trace:
PHP   1. {main}() /opt/rh/httpd24/root/var/www/html/testmoodle/lib/classes/task/badges_cron_task.php:0

What do I need to just run one of them via the command line?

Thanks



Average of ratings: -
In reply to Jerry Lau

Re: How to: Running specific cron tasks via command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To take a slightly different approach, have you seen $CFG->nomailever in config-dist.php ?

In reply to Howard Miller

Re: How to: Running specific cron tasks via command line

by Jerry Lau -

Hello Howard.


This was what I was leaning towards. Short of stopping the mail services, I assume that this setting is in config.php and set it to "0" or off state; and just run the cron.php which, because of this change would not send emails, correct?

In reply to Jerry Lau

Re: How to: Running specific cron tasks via command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
// When working with production data on test servers, no emails or other messages
// should ever be send to real users
$CFG->noemailever = true;    // NOT FOR PRODUCTION SERVERS!

You set it to 'true' (in config.php - add it if not there) so that mail is NOT sent. Got it? big grin

This is the correct way to disable mail on a test/development Moodle. 

In reply to Howard Miller

Re: How to: Running specific cron tasks via command line

by Jerry Lau -

Interesting... did not see this..


so if it was set to true and we turn it back to false, will the next run pick up all tasks that need to be executed on the next cron?

I would think so

In reply to Jerry Lau

Re: How to: Running specific cron tasks via command line

by Ken Task -
Picture of Particularly helpful Moodlers

On a dev site with that line set to send no email, that is active for as long as the line is readable .... and the config.php file is checked all the time.   So since it's a dev machine, if you ever want  to test something sending mail, just add '//' in front of that line.   That comments it out, Moodle will attempt to send mail.   Any config item legal to add to config.php takes precedence over any setting kept in DB tables and will either execute or not execute upon the next access by anything of config.php.   No need to reboot server nor restart apache, etc..

Dev site will mean changes ... often ... which Moodle needs to track/clean up etc.   So it would be best, even on a dev machine, to run the main cron job every minute - as per the recommenations.

'spirit of sharing', Ken

In reply to Jerry Lau

Re: How to: Running specific cron tasks via command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You're overthinking it I think. 

If that is set then it does everything *except* actually doing the send. That includes marking the mail as sent (even though it isn't). It simply switches off the mail sending. The messages will not be sent... 

In reply to Howard Miller

Re: How to: Running specific cron tasks via command line

by Jerry Lau -

Great.. thanks.. it works like a charm

In reply to Jerry Lau

Re: How to: Running specific cron tasks via command line

by Ken Task -
Picture of Particularly helpful Moodlers

+1 to what Howard said, but it is handy to know how to run just one of those task.  There is a command line script to run any of them:

cd /path/to/moodlecode/admin/tool/task/cli/

php schedule_task.php will bring up a listing/help.

If on linux and because of the 'backslashes' are 'escapes', run the specific task in double quotes:

php schedule_task.php --execute="\core\task\badges_cron_task"

Put the ones you want to run individually into one bash shell script at the same location.   Call it 'runem'.

Script contains three lines like the one above to execute each one.

Make 'runem' executable .... chmod u+x runem

Then all you have to do 'run 'em' is cd to the script, to run ... ./runem [ENTER]

'spirit of sharing', Ken


In reply to Jerry Lau

Re: How to: Running specific cron tasks via command line

by Jerry Lau -

by not sending the email, users may think this is the production version of our moodle. We want to 10000% make sure they do not receive any emails from this test platform.

we needed to do this so completion of a plugin will be complete and tested. Apparently a cron has to be run.


Thanks