Cron作业PHP脚本错误

时间:2016-08-01 16:54:37

标签: php cron

我用这个评论做了一个cron工作:" /home/username/public_html/cron.php"

我的php(cron.php)

    <?php 

    $server = 'localhost';
    $user = 'name';
    $pass = 'password';
    $dbname = 'dbname';

    $conn = new mysqli($server, $user, $pass, $dbname);

    $sql = "Update table;";
    $conn->query($sql);

    $conn->close();
    ?>

这是错误:

/home/username/public_html/cron.php: line 1: ?php: No such file or directory
/home/username/public_html/cron.php: line 3: =: command not found
/home/username/public_html/cron.php: line 4: =: command not found
/home/username/public_html/cron.php: line 5: =: command not found
/home/username/public_html/cron.php: line 6: =: command not found
/home/username/public_html/cron.php: line 8: syntax error near unexpected token `('
/home/username/public_html/cron.php: line 8: `$conn = new mysqli($server, $user, $pass, $dbname);'

任何人都可以帮忙解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:0)

您的脚本被解释为shell脚本。要在命令行运行PHP脚本:

php /home/username/public_html/cron.php

或者将其添加到文件的顶部并使其可执行:

#!/usr/bin/php

(或者PHP可执行文件的路径)