Windows server: anyone have cron output working?
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.)
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?
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.
It simply won't output to a text file using the > switch. Ideas? Any way to trouble shoot this ?
It seems the task scheduler doesn't accept output redirection in the commands we specify there.
But there's always a trick for this Put the following commands (I'm using forward slashes instead of backslashes, as the code filter is eating them; make sure you use backslashes ):
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.
Any way to make the log rotate to a new file by itself?
Change Iñaki's batch file to something like:
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...
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.
That is great! Would you be happy to paste the code in here so we can all see it?
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
figured this out... in version 2.0 or later you have to call cron.php from a different directory.
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.
oh, specific code I used, windows server 2008r2, us english
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
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?
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.
Hello, How can I use this in Linux, I need create a Cron Log but I have linux
@Edgar ... linux is covered at:
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:
> /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