[英]Getting cron job error because of <?php tag
I've set up a cron job to run. 我已经设置了一个cron作业来运行。 It executes a php file which is named cronj.php But it doesn't work and cron job notification I get is:
它执行一个名为cronj.php的php文件但它不起作用,我得到的cron作业通知是:
/root/website/myworld/blabla/cronj.php: line 1: ?php: No such file or directory
And line 1 in that file is simply a php tag <?php
I don't know how 而该文件中的第1行只是一个php标签
<?php
我不知道如何
Cron is executing the file as if it was a shell script. Cron正在执行该文件,就像它是一个shell脚本一样。 Normally you would put in a shebang line (like
#!/usr/bin/env php
) at the top of the file so that the shell knows how to invoke it, but PHP doesn't like it - as it outputs everything outside its tags. 通常你会在文件的顶部插入一个shebang行(比如
#!/usr/bin/env php
),以便shell知道如何调用它,但是PHP不喜欢它 - 因为它输出了它之外的所有内容标签。 Thus, instead of this: 因此,而不是:
0 3 * * * /mypath/myscript.php ...
try this: 尝试这个:
0 3 * * * /usr/bin/env php /mypath/myscript.php ...
or use @Ravenex's trick. 或使用@Ravenex的伎俩。
EDIT I was just rightly admonished for assuming PHP behaves in a consistent way. 编辑我正确地告诫我假设PHP以一致的方式运行。 Apparently, shebang does work in PHP.
显然,shebang 确实在PHP 中工作。 My apologies to @chess007.
我向@ chess007道歉。
We use cron to run nightly tasks in a php facebook game. 我们使用cron在php facebook游戏中运行夜间任务。 We do it by using curl like this:
我们通过像这样使用curl来做到这一点:
/usr/bin/curl http://www.ourdomain.com/page.php
If I remember right we had some issues using localhost to try to avoid external lookups. 如果我没记错,我们在使用localhost时遇到了一些问题,试图避免外部查找。 Also we tried using php command line execution, which mostly worked but caused a few strange bugs.
我们也尝试使用php命令行执行,这主要起作用但引起了一些奇怪的错误。
Try to call the web url (http://.....). 尝试调用网址(http:// .....)。
It's apparently not parsing it as an PHP script. 它显然没有将其解析为PHP脚本。
Edit: Please show use the cronjob you used, to verify my hunch was right. 编辑:请显示使用您使用的cronjob,以验证我的预感是否正确。
Use this to set your cron and also give email address in your cron setting Cpanel so that you get an email when cron runs successfully 使用它来设置您的cron,并在您的cron设置Cpanel中提供电子邮件地址,以便在cron成功运行时收到电子邮件
wget -O - http://YOURSITE/cron.php wget -O - http://YOURSITE/cron.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.