当通过Cron或php调用Magento Cron .php时抛出错误


Magento cron.php throws error when called via Cron or php

我最近使用Magento托管我的网站,在设置我的cron.php后,我得到以下输出:

/home/xxx/webapps/abc/cron.php: line 1: ?php
: No such file or directory
/home/xxx/webapps/abc/cron.php: line 2: /**
: No such file or directory
/home/xxx/webapps/abc/cron.php: line 3: bin: command not found
/home/xxx/webapps/abc/cron.php: line 4: $'*'r': command not found
/home/xxx/webapps/abc/cron.php: line 5: bin: command not found
/home/xxx/webapps/abc/cron.php: line 6: $'*'r': command not found
/home/xxx/webapps/abc/cron.php: line 7: syntax error near unexpected token `('
/home/xxx/webapps/abc/cron.php: line 7: ` * This source file is subject to the Open Software License (OSL 3.0)

我在Webfaction上托管这个,似乎这个错误只发生在Webfaction上。我的另一个网站(使用相同的Magento版本)托管在另一个服务上运行良好。

即使我删除了cron.php中的以下行,它仍然不能工作:

/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage
 * @copyright  Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

我也试着把"shebang"放在

#!/usr/local/bin/php

并得到这个结果:

/usr/local/bin/php^M: bad interpreter: No such file or directory

我怀疑编码可能有问题。谁能帮我一下?

可能没有换行符那么多。

不同的操作系统在ASCII中使用不同的回车和换行字符组合。Windows使用回车+换行符(ASCII码0x0D+0x)作为换行符的分隔符。Linux/Unix只使用换行符(ASCII码0x0A)。

当您看到^M时,它通常告诉您文本中有一个不可打印的字符(除非您确实使用了插入符号+ M,这是不寻常的)。您可以在这里看到控制字符及其"插入符号"的映射(锚后的第一个表,列[b]): https://en.wikipedia.org/wiki/ASCII#Control_characters

考虑到^M是在Linux中不使用的回车字符,在Windows中换行之前使用,我的第一个想法是你的文件在Windows上被编辑,然后被推送到Linux服务器。Linux不能解释以Windows CRLF行结尾的shebang行,所以这解释了shebang问题。

然而,PHP被设计成在其解释器中处理CRLF,所以我怀疑这是第一个问题。您没有详细描述文件是如何执行的,所以我假定您正在遵循这些说明。如果我的假设是正确的,那么您正在使用下面描述的三个cron行:http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html#create-the-cron-job。在这种情况下,我猜是使用了错误的PHP版本。我认为在WebFaction上,如果你使用/usr/local/php,默认的PHP版本是5.2,而Magento 2.1需要PHP 5.6。这个可能是你的问题,但如果没有更多的细节,很难确定。