CronTab:没有运行我的PHP脚本?

最终编辑:我已经把这个移动到406错误与GET Cron作业?

编辑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似乎并没有运行。 它不会logging任何错误,也不会发送电子邮件。 你知道什么是错的吗?

谢谢!

编辑3:

我find了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 

我怎么能设置包含path,这样既可以在我的Linuxdebugging服务器和我的Linux在线服务器?

如果服务器上安装了GET(lwp-request)或curl,则可以在cron中使用Web版本

得到

 * * * * * GET http://localhost/cron/sendQueuedEmails.php > /dev/null 

curl

 * * * * * curl -o /dev/null http://localhost/cron/sendQueuedEmails.php 

听我的build议蚱蜢! 在这里,做到了这一点。 做这个简单的方法,并使用php的“/ usr / bin / php-cgi”的cgi版本。 如果你没有,请安装“apt-get install php5-cgi”

不要相信我? 尝试这个。

在你的目录下创build一个名为test.txt的文本文件,并在里面添加一些随机文本。 现在创build一个名为test的新目录,里面有一个名为test.php的文件,然后添加下面的代码:

<?php

include '../test.txt';

?>

你的目录结构应该是这样的:

 /yourdirectory /yourdirectory/test.txt /yourdirectory/test/test.php 

切换到根目录“cd /”并从命令行运行:

/ usr / bin / php -f /yourdirectory/test/test.php

接着

/ usr / bin / php-cgi -f /yourdirectory/test/test.php

看到不同?

testing某行是否可以用cron运行的更简单的方法是:

 env -i bash -i /dev/null <actual command> 

检查/ var / log / cron,看看它是否真的在运行。 如果是,请尝试sudo -u以查看是否有错误。

另外 – 我会尝试使用PHP可执行文件的完整path。 通常它是'/ usr / bin / php',但你可以发出'哪个php'来仔细检查你的系统。

您的input将如下所示:

          • 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

希望这可以帮助。

现在,这些脚本可能会发送他们的输出到bitbucket,你不知道他们为什么失败。 您的cronjobs可能未configuration为发送电子邮件,或系统上的电子邮件可能无法正常工作。

我不是发送cron输出的粉丝,部分是因为人们总是认为将输出垃圾邮件转为“root”是可以的,而我是收到这封邮件的人。 由于在大多数情况下,我们正在讨论1-2行输出,所以我build议你将cronjob的输出logging到syslog中。

 * * * * * { cd /var/www/cron && php -f sendBulletinEmails.php ;} |/usr/bin/logger -t sendBulletinEmails * * * * * { cd /var/www/cron && php -f sendSurveyEmails.php ;} |/usr/bin/logger -t sendSurveyEmails 

在包含path中添加包含“includes”文件夹的文件夹。 要find它使用locate swift_required.php

根据您的原始问题以及您发布的代码,您错过了一个需要以这种方式运行PHP脚本的shebang行。 尝试更改前几行:

 #!/usr/local/bin/php5 -q <?php // your script here ?> 

你应该有成功。

当然,用你的PHP安装的正确pathreplace/ usr / local / bin。 它可能会/可能不一样。 你只需要这个第一行来运行它,而不是从浏览器运行。

嘿,嘿,它挂着埃萨? 这里有两个解决你的问题。

  1. 在脚本中使用包含文件的完整path。 所有包含都需要这样做。 如果包含文件包含其他内容,则还需要更新这些内容。

include'../somefile.php'应该包含'/somedir/somefile.php'

  1. 如果#1不起作用,或者你不想使用完整的path来包括,然后尝试使用php-cgi而不是php cli版本。

另外请阅读本节的php手册,密切关注cli和cgi的区别。

http://www.php.net/manual/en/features.commandline.differences.php

CLI SAPI不会将当前目录更改为已执行脚本的目录!

踢@ss!