最后编辑:我已将其移至GET Cron Job 出现 406 错误?
编辑4:
我使用这个 cron 时得到了 406 错误页面!
这是 crontab(从 cPanel 复制):
* * * * * GET https://abc.com/cron/sendBulletinEmails.php >>
/home/abc/public_html/cron/logs/sendBulletinEmails.log
这是日志:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>406 Not Acceptable</title>
</head><body>
<h1>Not Acceptable</h1>
<p>An appropriate representation of the requested resource /cron/sendSurveyEmails.php could not be found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
我在运行 Linux 的虚拟机上安装了 php。我将 crontab 设置为:
* * * * * { cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails
但是,cron 似乎没有运行。它没有记录任何错误,也没有发送电子邮件。您知道哪里出了问题吗?
谢谢!
编辑3:
我发现了 cron 运行的日志。似乎没有什么问题,但它仍然不起作用或输出任何东西!
Aug 5 16:20:01 fiqsrv1 CRON[18543]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:21:01 fiqsrv1 CRON[18549]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:22:01 fiqsrv1 CRON[18554]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:23:01 fiqsrv1 CRON[18559]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:24:01 fiqsrv1 CRON[18564]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:25:01 fiqsrv1 CRON[18569]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:26:01 fiqsrv1 CRON[18574]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:27:01 fiqsrv1 CRON[18595]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:28:01 fiqsrv1 CRON[18601]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
Aug 5 16:29:01 fiqsrv1 CRON[18610]: (cgurnik) CMD ({ cd /var/www/cron && /usr/bin/php -f sendQueuedEmails.php ;} |/usr/bin/logger -t sendQueuedEmails)
编辑2:
现在,当我运行脚本时,它什么都不输出。
我想发布脚本来表明它总是输出一些东西,这就是为什么当我运行它而没有任何东西输出时我感到困惑(没有错误,没有输出)
<?php
require '../includes/common.php';
/*
* check that this cron job isn't already running (it can take a long time if there is a large email load, which it is meant for)
* if it is running, end the script
* if it is not running, continue
* set the global variable for this cron job to on
* get all queued emails that have their time to be sent in the past (and so they should be mailed out now)
* loop through them, checking to see if the user is still set to receive the email, and if so, sending it to them
* set the global variable for this cron job to off
*
* JUST IN CASE: put the script in a try catch after the email cron is set to running in globalvars so that it is always reset, even upon failure
*/
// check that this cron job isn't already running (it can take a long time if there is a large email load, which it is meant for)
if(GlobalVars::isEmailCronRunning()) {
echo "Already running! Aborted.";
exit; // if it is running, end the script
}
// if it is not running, continue
// set the global variable for this cron job to on
GlobalVars::set(GlobalVars::VAR_IS_EMAIL_CRON_RUNNING, 1);
try {
// get all queued emails that have their time to be sent in the past (and so they should be mailed out now)
$queuedEmails = Emails::getAllQueuedToSend();
// loop through them, checking to see if the user is still set to receive the email, and if so, sending it to them
$numEmailsSent = 0;
$numEmailsRecalled = 0;
foreach($queuedEmails as $email) {
if(Emails::shouldBeSentToUser($email)) {
Emails::sendQueuedEmail($email[Emails::id]);
$numEmailsSent++;
} else {
Emails::update($email[Emails::id], array(Emails::result => Emails::RESULT_NOT_SENT) );
$numEmailsRecalled++;
}
}
// set the global variable for this cron job to off
GlobalVars::set(GlobalVars::VAR_IS_EMAIL_CRON_RUNNING, 0);
} catch (Exception $e) {
// set the global variable for this cron job to off
GlobalVars::set(GlobalVars::VAR_IS_EMAIL_CRON_RUNNING, 0);
echo "Error: " . print_r($e);
}
if($numEmailsSent || $numEmailsRecalled) {
$details = "Sent " . $numEmailsSent . ". Recalled " . $numEmailsRecalled . ".";
echo nl2br($details);
ActionLogs::add(ActionLogs::CAT_CRON_JOBS, ActionLogs::TYPE_CRON_EMAILER_RUN, $details);
} else {
echo "No emails were sent.";
}
?>
编辑:我尝试运行它并得到以下结果:
Warning: require_once(/includes/Swift-4.0.6/lib/swift_required.php): failed to open stream: No such file or directory in /var/www/includes/common.php on line 31
Fatal error: require_once(): Failed opening required '/includes/Swift-4.0.6/lib/swift_required.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/includes/common.php on line 31
我如何设置包含路径以便它在我的 Linux 调试服务器和 Linux 实时服务器上都能工作?
答案1
如果服务器上安装了 GET(lwp-request)或 curl,则可以在 cron 中使用 Web 版本
得到
* * * * * GET http://localhost/cron/sendQueuedEmails.php > /dev/null
卷曲
* * * * * curl -o /dev/null http://localhost/cron/sendQueuedEmails.php
答案2
听从我的建议,蚱蜢!来过这里,做过这个。用简单的方法做,使用 php 的 cgi 版本“/usr/bin/php-cgi”。如果你没有,安装它“apt-get install php5-cgi”
不相信吗?试试这个。
在您的目录中创建一个名为 test.txt 的文本文件并在其中添加一些随机文本。现在创建一个名为 test 的新目录并在其中创建一个名为 test.php 的文件,然后添加以下代码:
<?php
include '../test.txt';
?>
你的目录结构看起来应该像这样:
/yourdirectory
/yourdirectory/test.txt
/yourdirectory/test/test.php
更改为根目录“cd /”并从命令行运行此命令:
/usr/bin/php -f /你的目录/test/test.php
进而
/usr/bin/php-cgi -f /你的目录/test/test.php
看到不同?
答案3
测试某行是否可以运行的更简单方法是cron
:
env -i bash -i /dev/null <actual command>
答案4
另外 - 我会尝试使用 php 可执行文件的完整路径。通常是“/usr/bin/php”,但您可以发出“which php”来为您的系统再次检查。
您的条目看起来会像这样:
- cd /var/www/cron; /usr/bin/php -f sendBulletinEmails.php
- cd /var/www/cron; /usr/bin/php -f sendSurveyEmails.php
此外,如果您仅更改目录以便文件位于当前目录中,则您可以随时修改 cron 为:
- /usr/bin/php -f /var/www/cron/sendBulletinEmails.php
- /usr/bin/php -f /var/www/cron/sendSurveyEmails.php
希望这可以帮助。