PHP文件不能从Cron运行

时间:2023-01-13 16:02:34

I have the following PHP script that establishes a connection to a MySQL database and runs a SQL query to grab 4 fields, then creates a comma separated text file from the output. If I place this PHP file in my public_html folder and browse to it from my web browser, it functions correctly. However I want place this PHP script in a unique folder on the same level as public_html, and call it via a CRON job in my Cpanel. When I do this, I get the attached error.

我有以下PHP脚本,该脚本建立到MySQL数据库的连接并运行SQL查询以获取4个字段,然后从输出创建一个逗号分隔的文本文件。如果我将这个PHP文件放在我的public_html文件夹中,并从web浏览器中浏览到它,那么它的功能是正确的。但是,我希望将这个PHP脚本放在与public_html相同级别的唯一文件夹中,并通过Cpanel中的CRON作业调用它。当我这样做时,我得到了附加的错误。

script location /home2/mywebsite/whereIam/myscript.php

脚本的位置/我/ mywebsite / whereIam / myscript.php

text file output location /home2/mywebsite/public_html/text.txt

文本文件输出位置/home2/mywebsite/public_html/text.txt

Cron Job 0 0 * * * /home2/mywebsite/whereIam/myscript.php

Cron作业0 * * * /home2/mywebsite/where eiam /myscript.php

Error Message
/home2/mysite/whereiam/myscript.php: line 1: ?php: No such file or directory
/home2/mysite/whereiam/myscript.php: line 2: syntax error near unexpected token (' /home2/mysite/whereiam/myscript.php: line 2: $fh = fopen("/home2
/mywebsite/public_html/text.txt", "w");'

错误消息/我/ mysite / whereiam / myscript。php:没有这样的文件或目录/home2/mysite/whereiam/myscript。php:第2行:语法错误接近意外令牌(' /home2/mysite/whereiam/myscript)。php:第2行:$fh = fopen("/home2 /mywebsite/public_html/text ")。txt”、“w”);

<?php
    $fh = fopen("/home2/mywebsite/public_html/test.txt", "w");
    $con = mysql_connect("localhost","user_readoly","SomePassword");
    mysql_select_db("nameofDatabase", $con);

    /* insert field values into data.txt */

    $result = mysql_query("SELECT `field_129`, `field_131`,`field_26` FROM  `a_custom_database_5` WHERE `field_129` <> ''");   
    while ($row = mysql_fetch_array($result)) {          
        $num = mysql_num_fields($result) ;    
        $last = $num - 1;
        for($i = 0; $i < $num; $i++) {            
            fwrite($fh, $row[$i]);                       
            if ($i != $last) {
                fwrite($fh, ",");
            }
        }                                                                 
        fwrite($fh, "\n");
    }
    fclose($fh);
?>

2 个解决方案

#1


2  

There are two possibilities:

有两种可能性:

  • Change the cronjob to:

    改变的计划:

    0 0 * * * php /home2/mywebsite/whereIam/myscript.php
    

    Notice that the php command has been added with as argument the filename. Otherwise, cron tries to execute the PHP file, but it doesn't know it should be executed as PHP.

    注意,php命令添加了文件名作为参数。否则,cron尝试执行PHP文件,但它不知道应该以PHP的形式执行。

  • There's a way to tell the OS the PHP file should be executed as PHP. Add this as a first line to the script:

    有一种方法可以告诉操作系统PHP文件应该作为PHP执行。把它作为第一行添加到脚本中:

    #!/usr/bin/php
    

    Also set the permissions such that the file is executable. Now, you could execute it from a shell as if it's a command - and also the cronjob works without modification.

    还要设置允许文件可执行的权限。现在,您可以从shell中执行它,就像它是一个命令一样——而且cronjob也可以不需要修改。

    This is of course assuming the PHP executable is in /usr/bin/php. You can use the command which php to find the location on your system.

    当然,这是假设PHP可执行文件在/usr/bin/ PHP中可以使用php命令找到系统上的位置。

Which to choose is a matter of personal preference I suppose. I like the second option because it allows you to execute the file from the command line without explicitly using the php command. However, with the second option moving to another server with possibly a different path to php means you have to edit the file - which isn't needed with the first option.

我想,选择哪一个是我个人的偏好。我喜欢第二个选项,因为它允许您从命令行执行文件,而不用显式地使用php命令。但是,随着第二个选项转移到另一个服务器,可能会有不同的php路径,这意味着您必须编辑文件——这在第一个选项中是不需要的。

#2


1  

Try this

试试这个

0 0 * * * /usr/bin/php /home2/mywebsite/whereIam/myscript.php

0 * * * * /usr/bin/php /home2/mywebsite/where eiam /myscript.php

And I would also redirect the output to a log file so then change it to this

我还会将输出重定向到一个日志文件然后将它改为这个

0 0 * * * /usr/bin/php /home2/mywebsite/whereIam/myscript.php >> /path/to/log.php 2&>1

0 * * * * /usr/bin/php /home2/mywebsite/whereIam/myscript。php >路径> / / /日志。php 2 & > 1

This is assuming that php is at /usr/bin/php

假设php在/usr/bin/php

#1


2  

There are two possibilities:

有两种可能性:

  • Change the cronjob to:

    改变的计划:

    0 0 * * * php /home2/mywebsite/whereIam/myscript.php
    

    Notice that the php command has been added with as argument the filename. Otherwise, cron tries to execute the PHP file, but it doesn't know it should be executed as PHP.

    注意,php命令添加了文件名作为参数。否则,cron尝试执行PHP文件,但它不知道应该以PHP的形式执行。

  • There's a way to tell the OS the PHP file should be executed as PHP. Add this as a first line to the script:

    有一种方法可以告诉操作系统PHP文件应该作为PHP执行。把它作为第一行添加到脚本中:

    #!/usr/bin/php
    

    Also set the permissions such that the file is executable. Now, you could execute it from a shell as if it's a command - and also the cronjob works without modification.

    还要设置允许文件可执行的权限。现在,您可以从shell中执行它,就像它是一个命令一样——而且cronjob也可以不需要修改。

    This is of course assuming the PHP executable is in /usr/bin/php. You can use the command which php to find the location on your system.

    当然,这是假设PHP可执行文件在/usr/bin/ PHP中可以使用php命令找到系统上的位置。

Which to choose is a matter of personal preference I suppose. I like the second option because it allows you to execute the file from the command line without explicitly using the php command. However, with the second option moving to another server with possibly a different path to php means you have to edit the file - which isn't needed with the first option.

我想,选择哪一个是我个人的偏好。我喜欢第二个选项,因为它允许您从命令行执行文件,而不用显式地使用php命令。但是,随着第二个选项转移到另一个服务器,可能会有不同的php路径,这意味着您必须编辑文件——这在第一个选项中是不需要的。

#2


1  

Try this

试试这个

0 0 * * * /usr/bin/php /home2/mywebsite/whereIam/myscript.php

0 * * * * /usr/bin/php /home2/mywebsite/where eiam /myscript.php

And I would also redirect the output to a log file so then change it to this

我还会将输出重定向到一个日志文件然后将它改为这个

0 0 * * * /usr/bin/php /home2/mywebsite/whereIam/myscript.php >> /path/to/log.php 2&>1

0 * * * * /usr/bin/php /home2/mywebsite/whereIam/myscript。php >路径> / / /日志。php 2 & > 1

This is assuming that php is at /usr/bin/php

假设php在/usr/bin/php