General help

Windows server: anyone have cron output working?

 
 
Picture of Lael ...
Windows server: anyone have cron output working?
 
I've unsuccessfully attempted to get cron outputting to a text file. Anyone have this working at all?
 
Average of ratings:Useful (1)
Picture of Iñaki Arenaza
Re: Windows server: anyone have cron output working?
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers
In Unix/Linux it's rather easy: just redirect standard output and error output to a file.

Say you current moodle cron line looks like:


*/5 * * * * www-data /usr/bin/php5 -f /var/www/moodle/admin/cron.php


Just change it to look like (everything should be in a single line, it may display wrapped here):


*/5 * * * * www-data /usr/bin/php5 -f /var/www/moodle/admin/cron.php > /some/directory/where/www-data/can/write/cron.output 2> /some/directory/where/www-data/can/write/cron.output


where www-data is the user your web server runs with (www-data in Debian/Ubuntu, httpd in Red Hat, etc.)

Saludos,
Iñaki.
 
Average of ratings:Useful (2)
Tim at Lone Pine Koala Sanctuary
Re: Windows server: anyone have cron output working?
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers
One dirty trick is to make a course in your Moodle that only admins can get into, suppose that is the course with id 123. Then get cron to write the output to moodledata/123/cronlogs/{datestamp}.txt. That way, you can browse the cron output through the Moodle UI. But you have a security problem if anyone you don't trust manages to get enrolled in that course.
 
Average of ratings:Useful (2)
Picture of Lael ...
Re: Windows server: anyone have cron output working?
 
Thank you both for your quick responses....

so, let me explain more - I really didn't do a great job of asking a question that could be answered well.

Looking at: http://docs.moodle.org/en/Cron in the "Managing Cron on Windows" section.

It describes using php.exe as a scheduled task (we are running a wamp system - hence the windows based servers forum) to run cron - which works great. The terminal window pops up every 5 mins and runs the tasks just fine (except course backups which I still am having issues with and haven't been able to work out why...).

However - the output to text method described:
c:\php\php.exe -f c:\moodle\admin\cron.php > c:\moodle\admin\cron.log

doesn't work. Nothing gets written to the file. I am wondering if it is a settings problem, php problem or what?

I have read that using php-win.exe by design is 'silent' (no output), but that doesn't explain why I can't get php.exe to work.

So... any ideas?


 
Average of ratings: -
Picture of Richard Enison
Re: Windows server: anyone have cron output working?
 
L,

Are you sure your PHP interpreter is installed in c:\php? What happens if you leave off the "> c:\moodle\admin\cron.log"? Does it do anything? Does it produce output somewhere? And BTW, do you have a directory called moodle, with a subdirectory called admin, on your c: drive? It is understood that "c:\moodle" refers to the pathname of your main Moodle directory, and you should substitute that in the command shown. The same applies to c:\php\php.exe.

RLE
 
Average of ratings: -
Picture of Lael ...
Re: Windows server: anyone have cron output working?
 
Hi richard - yes, I've adapted the command for our install. Cron runs fine and does produce the terminal window showing it running each time it runs.

It simply won't output to a text file using the > switch. Ideas? Any way to trouble shoot this ?
 
Average of ratings: -
Picture of Iñaki Arenaza
Re: Windows server: anyone have cron output working?
Group DevelopersGroup Documentation writersGroup Particularly helpful Moodlers

It seems the task scheduler doesn't accept output redirection in the commands we specify there.

But there's always a trick for this smile Put the following commands (I'm using forward slashes instead of backslashes, as the code filter is eating them; make sure you use backslashes smile):

@echo off c:/php/php.exe -f c:/path/to/moodle/admin/cron.php >> c:/moodle-tasks/moodle-cron.log

in a batch file batch file (say, C:\moodle-tasks\moodle-cron.cmd), and then specify the path of this batch file as the task to run.

P.S. By using '>>' instead of simply '>' we keep all the cron logs in the file, instead of overwriting them with just the last run. As this can grow the log file pretty fast, make sure you rotate/clean it regularly.

Saludos, Iñaki.

 
Average of ratings:Useful (1)
Picture of Lael ...
Re: Windows server: anyone have cron output working?
 
Inaki - Thank you!

Will try this today.

Lael
 
Average of ratings: -
Picture of Lael ...
Re: Windows server: anyone have cron output working?
 
Brilliant! Thank you!

Any way to make the log rotate to a new file by itself? smile
 
Average of ratings: -
Sketch...
Re: Windows server: anyone have cron output working?
Group Particularly helpful Moodlers

Change Iñaki's batch file to something like:

@echo off
C:\php\php.exe -f C:\path\to\moodle\admin\cron.php >> C:\Moodle\%date:~0,4%%date:~5,2%%date:~8,2%.log

It takes characters 1-4 (year), 6-7 (month) and 9-2 (day) of the %date% variable, concatenates them and uses the final value, the system date, as the file name.

The output will be a text file called 20100111.log (or whatever the current date is) in the C:\Moodle folder, so each day you should get a new log file.

This might only work on the Windows OS and I suspect that it may be dependant on your Regional Settings. It works on my computer with Regional Settings set to English South Africa (which is the same as English UK, i.e. YYYY/MM/DD). If you don't get the correct file name using the above batch command then you need to change the 0, 5 and 8 in %date:~0,4%%date:~5,2%%date:~8,2% so that it picks up the correct position of the Year, Month and Day parts of the %date% variable for your Regional Settings. 

%date:~0,4%%date:~5,2%%date:~8,2% is actually just the %date% variable without the slashes and the time...

 
Average of ratings:Useful (2)
Picture of Brent Jones
Re: Windows server: anyone have cron output working?
 
Hi Lael -

I do the log rotation with an "if" statement in my batch file looking for a condition such as midnight, then executing an "del" or a "rename" of my log file before it gets written to with the ">>" operator. When the condition is not met, the existing file gets appended to. When the condition is met, the file is deleted or renamed and a new log file is started with the same ">>" operator.

Cheers,
Brent



 
Average of ratings: -
Picture of Lael ...
Re: Windows server: anyone have cron output working?
 
Hi Brent,

That is great! Would you be happy to paste the code in here so we can all see it?

Lael
 
Average of ratings: -
Picture of shailesh jai
Re: Windows server: anyone have cron output working?
 

Hello Friends,

i try all things whatever you wrote in your discusssion but still i am getting any log file. following are the code .

D:/wamp/bin/php/php5.2.6/php.exe -f D:/wamp/www/moodle/admin/cron.php > D:/wamp/www/moodle/admin/moodle-cron.log

 

in my case i install wamp on D drive

 
Average of ratings: -
Sketch...
Re: Windows server: anyone have cron output working?
Group Particularly helpful Moodlers

Did you put that code in a batch file?

 
Average of ratings: -
Picture of Jeremy Bascom
Re: Windows server: anyone have cron output working?
 

figured this out... in version 2.0 or later you have to call cron.php from a different directory.

From: http://docs.moodle.org/en/Administration_via_command_line

Running cron via command line

In versions 1.x, you could execute admin/cron.php either from command line or via the web. Since Moodle 2.0, only admin/cli/cron.php script can be run via command line

Once I started using that path, everything started working.

 
Average of ratings: -
Picture of Jeremy Bascom
Re: Windows server: anyone have cron output working?
 

oh, specific code I used, windows server 2008r2, us english

@echo off
C:\php\php.exe -f C:\path\to\moodle\admin\cli\cron.php >> C:\moodlelogs\%date:~10,4%%date:~4,2%%date:~7,2%.log

 
Average of ratings:Useful (1)
Picture of Scott Karren
Re: Windows server: anyone have cron output working?
Group Particularly helpful Moodlers

I am using Windows 2008 R2 datacenter with Moodle 2.1.3.  I am having some problems with cron output as well.  I used Jeremy's code, modified it to match my program paths and setup a scheduled task for it.  When I run the task the log file is created but it is blank, which makes me wonder if Cron is running at all.  Any ideas on what might be happening?

Update here - I just checked the scheduled task last run result and it is say 0x1, what does that mean, other than something is not working?

Scott Karren

 
Average of ratings: -
Picture of Scott Karren
Re: Windows server: anyone have cron output working?
Group Particularly helpful Moodlers

Ok, I got this working.  I figured out that because I have PHP installed in a place other than c:\php I needed to put the command in quotes.  After that it worked fine.  

 
Average of ratings:Useful (1)
Picture of Edgar Conrado Hernandez
Re: Windows server: anyone have cron output working?
 

Hello, How can I use this in Linux, I need create a Cron Log  but I have linux

 
Average of ratings: -
Picture of Ken Task
Re: Windows server: anyone have cron output working?
Group Particularly helpful Moodlers

@Edgar ... linux is covered at:

http://docs.moodle.org/25/en/Cron

If one doesn't use > /dev/null but a re-direct for linux logs ... you didn't mention which Linux distro.  CentOS, for example, uses /var/log/ for it's logging directory.  Other OS's might use something else.

One simply uses Linux OS date:

$(date +%Y%m%d%-H%M%S)

> /var/log/cron-$(date +%Y%m%d%-H%M%S).log

See your linux man (online manual) for date.

This means, however, a single cron... .log file per day.

Cron does send EMail notifications if run simply (ie, without any re-direct to a log file with date/time stamp), however, and one could setup a simple routing rule for one's inbox to have all cron messages routed to a particular folder.

'spirit of sharing', Ken

 

 
Average of ratings: -