简体   繁体   English

PHP文件可以手动正常运行,但在Cron Job上表现异常

[英]PHP File Runs Fine Manually But behaves unexpected on Cron Job

I have a dreamhost web panel and I am setting a cronjob to run my php script, but it shows weird errors, it looks like that it's not executing the file , but it's reading the file. 我有一个dreamhost Web面板,并且正在设置cronjob来运行我的php脚本,但是它显示了奇怪的错误,似乎它没有执行该文件,但是正在读取该文件。 The error is shown below, it comes as email to me 该错误如下所示,它作为电子邮件发送给我

?php: No such file or directory [somesessionvariable]=1573: command not found line 3: syntax error near unexpected token "include/mydb.php"' line 3: require_once("include/mydb.php"); ?php:没有这样的文件或目录[somesessionvariable] = 1573:找不到命令第3行:意外令牌"include/mydb.php"' line 3:附近的语法错误"include/mydb.php"' line 3: require_once(“ include / mydb.php”);

This is the subject of the mail: 这是邮件的主题:

Cron /usr/local/bin/setlock -n /tmp/cronlock.3782446772.128284 sh -c $'/path /to/file/cases_cron.php' Cron / usr / local / bin / setlock -n /tmp/cronlock.3782446772.128284 sh -c $'/ path /to/file/cases_cron.php'

the permission of the file is 755 and i have copied the path from other perfectly running cronjobs, so no issue to the file path as it's getting the file contents. 文件的权限为755,我已经从其他运行良好的cronjobs复制了路径,因此文件路径没有问题,因为它正在获取文件内容。 I am not getting where the problem is 我没有找到问题所在

I should try to use an absolute path to your include file. 我应该尝试使用包含文件的绝对路径。

I'm sure the problem is about the include path not being set the same in file call and cron call. 我确定问题出在文件调用和cron调用中,包含路径设置不相同。

You can do something like this : 您可以执行以下操作:

require_once dirname(__FILE__)."/include/mydb.php";

__FILE__ is the path of the file where this line is executed, dirname() return the absolute path where the file is located. __FILE__是执行此行的文件的路径, dirname()返回文件所在的绝对路径。

Of course, you can use, as you want require , require_once , include , include_one 当然,您可以根据require使用requirerequire_onceincludeinclude_one

Your paths are probably off. 您的路径可能已关闭。 Try to either avoid relative paths (include/file.php instead of /var/www/project/include/file.php) or change your working directory with chdir() . 尝试避免相对路径(用include / file.php代替/var/www/project/include/file.php)或使用chdir()更改工作目录。

您也可以在您的cronjob中输入“ cd”命令,即:

cd /path/to/phpfile && php -f file.php

Ok, Guys, Thanks for all of your support, I am finally able to sort out the matter. 好的,伙计们,感谢您的全力支持,我终于可以解决问题了。

I was missing the following line on the top of the file 我在文件顶部缺少以下行

#!/usr/local/bin/php

Adding this line at the top of the php file, sorted my problem. 在php文件的顶部添加这一行,对我的问题进行了排序。 But i don't know that what this line means to the script. 但是我不知道这行对脚本意味着什么。 But i copied it from other working crons. 但是我从其他工作专家那里复制了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM