Make your theme do cron for you.

Make your theme do cron for you.

by Kevin Hughes -
Number of replies: 33
Here's a snippet of code for those who have no cron and don't want to install one.  Just cut & paste it into the header.html of your theme. 

It checks how long it's been since the last time cron.php was run and runs it if it was longer than an hour ago.  To change how often it runs change the 3600 to however seconds you want between each run.

It also dumps the output from the cron.php run into cron.time in your moodle data directory so you can check it.

Hope it's of use to someone.

 <?php
    if(file_exists($CFG->dataroot."/cron.time") && time()-filemtime($CFG->dataroot."/cron.time")>3600){
    $ch = curl_init($CFG->wwwroot."/admin/cron.php");
    $fp = fopen($CFG->dataroot."/cron.time", "w");
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
 }
 ?>
Average of ratings: -
In reply to Kevin Hughes

Re: Make your theme do cron for you.

by Art Lader -
Dear Kevin,

I am sure that your snippet will be "of use to someone." Thank you very much for posting it. smile

-- Art
In reply to Kevin Hughes

Re: Make your theme do cron for you.

by Art Lader -
Is anyone using this wonderous snippet? It seems to me like it would make many lives a tad less frustrating...

-- Art
In reply to Art Lader

Re: Make your theme do cron for you.

by Sean S -
I shall be adding this right now to a clients site. If this works like it should, then I vote for this being added to the core code with a gui interface in the site variables page.

*edit*
just noticed it gets added to the theme header. Not good for me, as I'm adding it to a clients site and don't want to restrict him to only one theme.
In reply to Sean S

Re: Make your theme do cron for you.

by Chris Lamb -

You could copy and paste it into each theme in Moodle.  The only snag with that would be that if you wanted to change the timing you'd have to change the 3600 to something else in every theme.  The easy way round that would be to make this code snippet a standalone routine, called something like 'runcron.php', and drop a one-line 'include' statement in each theme to call it.  Changing the timing in the runcron.php file would then work whichever theme called it.

Am I right in thinking that this snippet will only execute when someone goes to Moodle and therefore triggers the theme's PHP file?  This could potentially mean that some cron jobs might be missed last thing at night - if the last user is on the site, say, 45 minutes after the last cron ran, he won't trigger it, so any forum posts etc from that 45 minute period waiting to be sent won't be processed until the first user logs on to the site the following morning.  That shouldn't be much of a problem, but it's perhaps worth bearing in mind.

Can cron be triggered from an external program, for example something written in Visual Basic?  If so, a VB program which runs the cron could be added to the server's (or for that matter any other computer's) scheduled tasks, and run every hour regardless of activity on Moodle.  If VB can be made to run cron, if someone can tell me what it needs to actually trigger I'll have a go at writing the code.

Regards

Chris

In reply to Chris Lamb

Re: Make your theme do cron for you.

by Art Lader -
Can cron be triggered from an external program, for example something written in Visual Basic? If so, a VB program which runs the cron could be added to the server's (or for that matter any other computer's) scheduled tasks, and run every hour regardless of activity on Moodle. If VB can be made to run cron, if someone can tell me what it needs to actually trigger I'll have a go at writing the code.

That sounds very cool, Chris.

-- Art
In reply to Art Lader

Re: Make your theme do cron for you.

by Art Lader -
It would just have to execute http://www.something.com/admin/cron.php, I think.

-- Art
In reply to Art Lader

Re: Make your theme do cron for you.

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators
Yes, your right !

And if you have a windows computer always running, you can call your cron (or several) with WebMon, without difficulty wink

Hope this helps,
In reply to Chris Lamb

Re: Make your theme do cron for you.

by Jan Dierckx -

There is a Moodle cron for windows program somewhere on this page

If you want to use the windows task scheduler, a more difficult option would be to download wget for windows from unxutils or from here

Then you can add a scheduled task to windows like this ...

C:\unxutils\usr\local\wbin\wget.exe -o log.txt http://yourmoodle.com/admin/cron.php

In reply to Chris Lamb

Re: Make your theme do cron for you.

by Chris Lamb -

Attached to this message should be a zip file containing two other files.  One is Moodle Cron.exe, and the other is crons.dat.

Moodle Cron.exe is a small program written in Visual Basic 6, which will trigger cron jobs on a schedule.  It handles multiple crons, (I don't know what the upper limit might be - I'm currently running 14 on it without any problems), and is configurable so you can choose how often to run and whether to keep a log file.  Note that for this to run you'll need the VB6 runtime files installed, which should be available as a free download from Microsoft's website.

crons.dat is the configuration file.  In it you set up which crons to run, how often to run them, and whether to keep a log file.  The exact syntax of how to do it is contained at the top of the crons.dat file, and there's a dummy cron job set up in the file as well to show how to do it.  Note that this file has to be in the same folder as the Moodle Cron.exe file.

The usual disclaimers apply - we run an up-to-date version of McAfee here, so it should be free of viruses, but it's your responsibility to check before you run it on your machine.  I'm not responsible for any problems it may cause.  I don't know if it will run on a server, as so far I've only run it on my desktop.  This bit of software is released under the Moodle/GNU licence.

If anyone has any suggestions for improvements please let me know, and I'll either look at it myself or release the source code so others can play with it.

Chris

In reply to Chris Lamb

Re: Make your theme do cron for you.

by Chris Lamb -

It appears that one of the required OCX files isn't included in the VB6 runtimes from Microsoft.  It is, however, available from here, with instructions on how to install it.

http://www.annoyances.org/exec/forum/win98/1072967802

I've created an installer package for the MoodleCron program which should install everythig you need, but unfortunately it's 1.5MB, which is three times the upload limit on this forum!  If anyone wants it please send me your email address and I'll send it to you as an attachment.

Chris

In reply to Sean S

Re: Make your theme do cron for you.

by Tim Tacker -
I second the motion to add this snippet to the core code.  It would be nice if this were an option upon initial setup.  The suggestion should be made that a cron job should be used; however, if the user is unable (or does not know how) to setup a cron job, this could be defaulted to run on every page hit, at the cost of a performance hit.  The only problem that I have with this snippet is that it is theme-specific.  Isn't there any way to make this snippet run sitewide?
In reply to Tim Tacker

Re: Make your theme do cron for you.

by kathy hooper -

hi

we have been having trouble getting cron to work so I tried your idea of putting it in the header - tho I put it in the footer as it can do less harm there. I get the following error message

Fatal error: Call to undefined function: curl_init() in C:\XAMPP\xampp\htdocs\theme\aggsfront\footer.html on line 22

Any ideas of how to fix this?

kathy

In reply to Kevin Hughes

Re: Make your theme do cron for you.

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Kevin,

> " if(file_exists($CFG->dataroot."/cron.time")"

what happens the very first time that your little cron utility is run? cron.time does not exist yet, so the program quits, no?

I'm puzzled

Joseph

In reply to Joseph Rézeau

Re: Make your theme do cron for you.

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Answering my own question.
I have put an empty cron.time file in the /moodledata directory on my site... and now it's working.
Joseph
In reply to Joseph Rézeau

Re: Make your theme do cron for you.

by mark zuber -

What is cron.time file? I have created folder cron.time in moodledata, but I am getting the following error message:

Warning

: fopen(/home/content/W/E/B/WEBDRIVERED/html/moodledata/cron.time) [function.fopen]: failed to open stream: Is a directory in /home/content/W/E/B/WEBDRIVERED/html/LMS5/theme/orangechoc/header.html on line 21

Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in /home/content/W/E/B/WEBDRIVERED/html/LMS5/theme/orangechoc/header.html on line 22

Server Time: Wed, 07 Jun 2006 19:17:56 -0600

 

Starting activity modules

Processing module function assignment_cron ...done.

Processing module function forum_cron ...done.

Processing module function journal_cron ...done.

Processing module function workshop_cron ...done.

Finished activity modules

Updating languages cache

Running backups if required...

Checking backup status...INACTIVE

Backup tasks finished.

Cron script completed correctly

Execution took 1.2025 seconds

Warning

: fclose(): supplied argument is not a valid stream resource in /home/content/W/E/B/WEBDRIVERED/html/LMS5/theme/orangechoc/header.html on line 26
Mark
In reply to mark zuber

Re: Make your theme do cron for you.

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Mark,

Are you actually using the orangechoc theme?

>What is cron.time file? -> it's the file which is used by the CRON program.

>I have created folder cron.time in moodledata, but I am getting the following error message.

No, no, no. You must not create a cron.time folder. It seems you have not understood my post dated 24th february: "I have put an empty cron.time file in the /moodledata directory on my site... and now it's working."

Just use any simple text editor (Windows Notepad will do) to create an empty text file and save it with the name cron.time in your moodledata directory.

Joseph

In reply to Joseph Rézeau

Re: Make your theme do cron for you.

by mark zuber -
Hi. Joseph

Yes, I am using orangechoc theme.  I have created cron.time.txt  file  in moodledata, -no more error messages. How can I view the content of this cron.time file after the cron jobs have been done, should I open it in a browser or should I download it onto my desktop and view the file's content to make sure that the cron job works?

PS. Please excuse my limited English.

Thanks

Mark

In reply to mark zuber

Re: Make your theme do cron for you.

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi Mark,

The (empty) file to be created once only in your moodledata folder should be called cron.time, not cron.time.txt.

To view its contents, just download it to your desktop (using an FTP software, I personally use FileZilla) and open it. It will look something like this:

Server Time: Thu, 08 Jun 2006 22:08:56 +0200
Starting activity modules
Processing module function assignment_cron ...done.
Processing module function chat_cron ...done.
Processing module function dialogue_cron ...done.
Processing module function exercise_cron ...done.
Processing module function forum_cron ...done.
Processing module function journal_cron ...done.
Processing module function workshop_cron ...done.
Finished activity modules
Updating languages cache
Running backups if required...
Checking backup status...OK
Getting admin info
Deleting old data
Checking courses
Skipping deleted courses
0 courses
Sandbox
Next execution: Saturday, 10 June 2006, 05:00 PM
Course Fullname 101
Next execution: Saturday, 10 June 2006, 05:00 PM
Backup tasks finished.
Cron script completed correctly
Execution took 2.004774 seconds

Hope that helps,

Joseph

In reply to Joseph Rézeau

Re: Make your theme do cron for you.

by mark zuber -

OK, now it works great. Thanks again.

Cheers.

Mark

In reply to mark zuber

Re: Make your theme do cron for you.

by Nick Churchill -
Where can I find the moodledata directory?  I've got something called .fantasticodata/Moodle but that's the closest I can see.  Thanks, Nick
In reply to Joseph Rézeau

Re: Make your theme do cron for you.

by Manuel Ramirez -
Hi:

I am using the theme to cron. I placed an empty cron.time in moodledata, but nothing is being written in it. I see that the date of the archive modifies every 5 minutes (so i think the code is working (I set the time to 300)), but the archive is empty and the emails are not being sent. Can anybody help me please?

Manuel
In reply to Manuel Ramirez

Re: Make your theme do cron for you.

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
> I am using the theme to cron.
And you have written this at the end of your header.html file? :
<?php
if(file_exists($CFG->dataroot."/cron.time") && time()-filemtime($CFG->dataroot."/cron.time")>300){
$ch = curl_init($CFG->wwwroot."/admin/cron.php");
$fp = fopen($CFG->dataroot."/cron.time", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
?>
In reply to Joseph Rézeau

Re: Make your theme do cron for you.

by Manuel Ramirez -
Yes, I copied exactly that code into header.html :-S
Could it be a permission problem?

Manuel
In reply to Manuel Ramirez

Re: Make your theme do cron for you.

by Ger Tielemans -
It works for me under http but NOT under https on the same server, could that be the problem?
In reply to Ger Tielemans

Re: Make your theme do cron for you.

by Manuel Ramirez -
I am using http for the address, so i don't think that could be the problem... :-S
Could it be that my hosting service blocks come actions?

Manuel
In reply to Ger Tielemans

Re: Make your theme do cron for you.

by Greg Lyon -
$CFG also has $CFG->httpswwwroot, perhaps it would be a better var to use in this case?  Or perhaps I'm grasping at straws wink
In reply to Greg Lyon

Re: Make your theme do cron for you.

by Manuel Ramirez -
Hi:

Thanks for your suggestion. I tried that, but it didn't work.

Manuel
In reply to Kevin Hughes

Re: Make your theme do cron for you.

by Darrin Jackson -
Just saying thanks!

My host wont allow wget - for security reasons...so this will help. Does anyone know of a way to use cron jobs to run this without using wget?
In reply to Darrin Jackson

Re: Make your theme do cron for you.

by Jan Dierckx -

Making your theme do cron for you is meant for people whose host doesn't allow any cron jobs.

If your hosting provider allows cron jobs, but doesn't allow wget to be used, there are some alternatives. Ask them if you are allowed to run lynx or a commandline version of php. Then you can setup a cronjob to run

/path/to/php /yourweb/yourmoodle/admin/cron.php 

every 5 or 10 minutes.

More examples / explanations

In reply to Jan Dierckx

Re: Make your theme do cron for you.

by Darrin Jackson -
Thank You.

I wanted to carefully consider all options before replying. You made me research more options, and I found one that seemed to work.

php --quiet /home/yourpath/public_html/yourdirectory/filenamephp

I put this into my cron jobs, modified for this file and it seemd to work.

I found it in a forum post by Guardian2003.

In reply to Kevin Hughes

Re: Make your theme do cron for you.

by Andrew Petrin -

Dear Kevin!

 

Sorry for the stupid question, but I'm using 2.2.1 moodle version and don't have any "Header.html", but still need this tool. Where should I put the script, or are there any alternative?

Thank you, sorry for noobing! shy

In reply to Andrew Petrin

Re: Make your theme do cron for you.

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Firstly Andrew, as a 6 year old thread this topic would be referring to 1.x themes (1.7/1.8???)

But as things have moved on from there, very few webhosts now do not have cron jobs available for you to run on the server itself (and if you are hosting yourself adding a cron job to either a Linux or Windows server should be very straightforward), so why implement one which is theme specific and doesn't work if you change themes.

On a more general point, code which is identified as going in the header.html of an old 1.x theme would now go in the layout files (usually general.php/default.php etc.)and code designed for 1.x themes will not necessarily work on 2.x sites.

In this case, you would possibly have to put it into all of the layout files your theme uses? mmm not sure - if your site was being visited frequently enough you might get away with only putting it in the frontpage.php??? but then if your site is not visited (say a school site during the holidays) no cron job will be triggered at all.

I would suggest exploring other ways of triggering the cron jobs through your server or webhost rather than through the theme.

Richard