Cron作业PHP脚本失败但脚本将通过CLI或浏览器运行

时间:2023-01-19 01:11:47

I have several php scripts I am trying to set up (recently moved to a new server) they will run from the command line and through the browser but only one will run through cron the other seems to have a permissions problem, if the file is set to 644 I get this message from cron: /bin/sh: /home/xyz/public_html/scripts/update-script.php: Permission denied

我有几个我试图设置的PHP脚本(最近移动到新服务器),它们将从命令行和浏览器运行,但只有一个将通过cron运行,另一个似乎有权限问题,如果文件是设置为644我从cron收到此消息:/ bin / sh:/home/xyz/public_html/scripts/update-script.php:权限被拒绝

If I set permissions to 777 I get this message:

如果我将权限设置为777,我会收到以下消息:

/home/xyz/public_html/scripts/update-script.php: line 1: ?php: No such file or directory

/home/xyz/public_html/scripts/update-script.php:第1行:?php:没有这样的文件或目录

/home/xyz/public_html/scripts/update-script.php: line 2: syntax error near unexpected token `"includes/clsDatabase-list.php"'

/home/xyz/public_html/scripts/update-script.php:第2行:意外令牌附近的语法错误`“includes / clsDatabase-list.php”'

/home/xyz/public_html/scripts/update-script.php: line 2: ` require_once("includes/clsDatabase-list.php");'

/home/xyz/public_html/scripts/update-script.php:第2行:`require_once(“includes / clsDatabase-list.php”);'

yet the script runs from the command line and via browser AND i have another script that is almost identical to this one (calls for the same include on line 1, is located in exactly same folder) that will run through cron! So I know my paths and cron job that I setup in Cpanel are right. If I copy the working one in the command line, the copied version also fails to run via cron. Thanks!

然而,脚本从命令行和浏览器运行并且我有另一个脚本几乎与这个脚本相同(调用相同的包含在第1行,位于完全相同的文件夹中)将通过cron运行!所以我知道我在Cpanel设置的路径和cron工作是正确的。如果我在命令行中复制工作版,则复制的版本也无法通过cron运行。谢谢!

1 个解决方案

#1


1  

You need to add a shebang to your file to execute it directly:

您需要在文件中添加一个shebang才能直接执行它:

#!/usr/bin/env php
<?php
//...

Another option would be calling it like this in your cronjob:

另一种选择是在你的cronjob中调用它:

* * * * * php /home/xyz/public_html/scripts/update-script.php

Of course you'd replace the * * * * * with the actual crontab data unless you want it to run every minute.

当然,除非您希望每分钟运行一次,否则您将* * * * *替换为实际的crontab数据。

#1


1  

You need to add a shebang to your file to execute it directly:

您需要在文件中添加一个shebang才能直接执行它:

#!/usr/bin/env php
<?php
//...

Another option would be calling it like this in your cronjob:

另一种选择是在你的cronjob中调用它:

* * * * * php /home/xyz/public_html/scripts/update-script.php

Of course you'd replace the * * * * * with the actual crontab data unless you want it to run every minute.

当然,除非您希望每分钟运行一次,否则您将* * * * *替换为实际的crontab数据。