我要通过 cron-jobs.org 将基于 Web 的 cron 作业设置到我安装了 Moodle 的站点。当我通过命令行方式执行 cron 作业时,
/usr/bin/php path/to/moodle/admin/cli/cron.php
,我在执行和结果方面没有问题;但在我的 cron 任务的 cron-jobs 状态中,显示为“响应太大”。
我能做什么?
或者,您可以使用 Google Apps 脚本:https://developers.google.com/apps-script
1 - 创建一个 Google SpreadSheet
2 - 单击工具 > 脚本编辑器
3 - 复制并粘贴以下代码:(替换网址)
function runCron() {
var url = "http://yourmoodle.com.br/moodle/admin/cron.php";
var options = {
'method': 'get',
//muteHttpExceptions: true,
};
var maxRows= 1440;
try{
var response = UrlFetchApp.fetch(url, options).getContentText().substring(0,50000);
Logger.log(response);
} catch (e) {
Logger.log(e);
var response = e;
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// This logs the value in the very last cell of this sheet
sheet.insertRowAfter(1);
sheet.getRange('A2').setValue(new Date()).setNumberFormat("MM/dd/yyyy hh:mm");
sheet.getRange('B2').setValue(response);
var lastRow = sheet.getLastRow();
if(lastRow>maxRows){
sheet.deleteRows(maxRows, lastRow - maxRows)
Logger.log(response);
}
}
4 - 保存并编辑项目名称
5 - 单击编辑 > 当前项目的触发器
6 - 添加新触发器
7 - 将事件源设置为Time-driven,将基于时间的触发器类型设置为Minutes timer
8 - 保存并登录以授予您项目的权限。
如果您有选择,请不要使用基于 Web 的 cron。它很可能会在未来的 Moodle 版本中被删除。
...建议仅从命令行运行 cron 或设置 cron 密码以进行远程访问。
参考:
https://docs.moodle.org/38/en/Cron#The_web_based_Moodle_cron_command
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app