Getting cron job error because of <?php tag

不打扰是莪最后的温柔 提交于 2019-12-09 18:48:10

问题


I've set up a cron job to run. It executes a php file which is named cronj.php But it doesn't work and cron job notification I get is:

/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:


Cron is executing the file as if it was a shell script. 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. 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.

EDIT I was just rightly admonished for assuming PHP behaves in a consistent way. Apparently, shebang does work in PHP. My apologies to @chess007.




回答2:


We use cron to run nightly tasks in a php facebook game. We do it by using curl like this:

/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. Also we tried using php command line execution, which mostly worked but caused a few strange bugs.




回答3:


Try to call the web url (http://.....).

It's apparently not parsing it as an PHP script.

Edit: Please show use the cronjob you used, to verify my hunch was right.




回答4:


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

wget -O - http://YOURSITE/cron.php



来源:https://stackoverflow.com/questions/8626907/getting-cron-job-error-because-of-php-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!