Saving log file during a scheduled task (cron)

Saving log file during a scheduled task (cron)

by Thiago Kadooka -
Number of replies: 2

Hello people!


Im developing an admin tool plugin, that do a scheduled task once a day.

The problem is, i want to save a log file of all that process, a log that i made myself, so, in the "execute" function, i place all my logic there, and then in the end of the process, i do a fopen, fwrite and then a fclose to save my log file.


When the moodle cron execute, right a this exact point, i get this message:


A lock was created but not released at:

[dirroot]\lib\cronlib.php on line 99

if i remove the fopen, fwrite and fclose, everything works smoothly.
i tryed to use file_put_contents, i got the same error.

If i run this code outside of cron, it works just fine, and save my log file successfully.


What is the problem? moodle blocks file creation in cron or something?

Average of ratings: -
In reply to Thiago Kadooka

Re: Saving log file during a scheduled task (cron)

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hi Thiago,

This is done automatically for you by Moodle, assuming you are using 3.7 - see MDL-49399

In reply to Paul Holden

Re: Saving log file during a scheduled task (cron)

by Thiago Kadooka -
Yeah i know he saves that, but i need to save my own, since i save multiple logs generated from the operation (one for errors in operation and other for things going right), and i save the error list as json too, anyway, i have multiple reasons to save my own logs.

but them, moodle actually dont allow me to save files during cron execution?