Cron作业报错

Cron job report error

我使用以下代码获得了一个 cron 作业:

<?php
require "config.php";
$truncate = $mysqli->query("TRUNCATE rss_feed");

$cars = $mysqli->query("SELECT *,(SELECT title FROM companies WHERE companies.company_id = cars.company_id) AS car_company,(SELECT car_model FROM models WHERE models.model_id = cars.model_id) AS car_model FROM cars, companies WHERE cars.company_id = companies.company_id AND car_active = 'Active' AND rssPost IS NULL ORDER BY datetime DESC");
$result = $cars->num_rows;

if ($result > 0) {
    while ($row_cars = $cars->fetch_array()) {
        $title = $row_cars['car_company'] . ' ' . $row_cars['car_model'] . ' ' . $row_cars['car_cc'];
        $link = '/car.php?id=' . $row_cars['car_id'];
        $description = $row_cars['car_description'];
        $datetime = $row_cars['datetime'];
        $row_car_id = $row_cars['car_id'];

        $insert = $mysqli->query("INSERT INTO rss_feed (title, link, description, datetime) VALUES ('$title','$link','$description','$datetime')");
        $update = $mysqli->query("UPDATE cars SET rssPost='Yes' WHERE car_id = '$row_car_id'");

        if (!$insert) {
            printf("ERROR: %s\n", $mysqli->error);
        }
    }
    printf($result . " cars inserted");
} else {
    printf("No new cars inserted");
}

我已将 cPanel 设置为每 3 小时 运行 php 文件,但我收到以下错误消息:

/public_html/system/cron_rss.php: line 1: ?php: No such file or directory
/public_html/system/cron_rss.php: line 2: require: command not found
/public_html/system/cron_rss.php: line 3: syntax error near unexpected token `('
/public_html/system/cron_rss.php: line 3: `$truncate = $mysqli->query("TRUNCATE rss_feed");'

如果我 运行 通过 URL 文件,它可以正常工作,没有错误。我的 IDE 编辑确认没有可疑代码。我得到了文件的 755 CHMOD。

尝试让 cron 作业使用 wget 或 curl 来命中 URL

从第一行错误来看,您似乎没有使用 cron 正确调用脚本。

根据您的主机和您的可能性,您可以尝试其中之一:

以这种方式指定 cron 条目:

* * * * * /usr/bin/php <path_to_your_script> 

或者这样指定 cron 条目:

php -q <path_to_your_script>

或者你可以尝试在你的脚本的第一行这样写:

#!/usr/bin/php

您还可以查看此讨论是否有帮助:

discussion