A Better Way of Running Moodle Cron?!

Moodle docs:
https://docs.moodle.org/35/en/Cron

First, running cron regularly is a *MUST* … period.

IMPORTANT: Do not skip setting up the cron process on your server for your Moodle. Your site will not work properly without it.

Later in the doc, this …

To run the cli cron script every 1 minute, add the line:

*/1 * * * * /usr/bin/php /path/to/moodle/admin/cli/cron.php >/dev/null

NOTE: the final >/dev/null sends all the output to the ‘bin’ and stops you getting an email every 1 minute.

Is there yet a better way? Yes!

touch /var/log/moodle_cron.log

That creates a blank/contains nothing moodle_cron.log file to which one will be sending the cron job output.

Next set up the cron … same time */1 * * * * but we output to a log file.

/usr/bin/php /var/www/html/admin/cli/cron.php >/var/log/moodle_cron.log 2>&1

Breaking that down … the > /var/log/moodle_cron.log redirects to the log file.  The log file is rewritten every run. Thus it never grows where one would have to rotate the log.

The 2>&1. makes the cron job ‘slient’ … meaning it doesn’t send mail to admin user every minute and thus filling up an inbox as well as filling up a drive!

Once that is saved, one can ‘watch’ the cron job in real time via:

tail -f /var/log/moodle_cron.log to see it execute … or error.

If you have multiple Moodles, make multiple cron jobs that log to files for each instance:

Example: a moodle 34 and a 35 on stame server.

touch /var/log/moodle34_cron.log
touch /var/log/moodle35_cron.log

Either tail -f one at a time, or install multitail

https://www.vanheusden.com/multitail/

Once multitail installed, one couuld watch both crons at the same time:

nano /usr/local/bin/watchcrons

In ‘watchcrons’, a single line:

/usr/bin/multitail -i /var/log/moodle34_cron.log -i /var/log/moodle35_cron.log

Save the watchcrons file. Make it executable.
To execute: watchcronS [ENTER]

Also note: multiple moodles means multiple crons … rather than setting them both up to run every minute, one might have to give a little wiggle room between running.

Every 2 minutes – 1 minute apart … why? Some of the jobs use up M’s of memory. How much memory do you have? Matter of fact, the docs for Cron at Moodle.org even suggest that for larger sites, a dedicated server for the running of crons. Posssible to do since version 3.4 of Moodle.

Leave a Reply