Cron作业报告错误

时间:2015-04-06 21:16:51

标签: php cron

我使用以下代码获得了一个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的文件。

2 个答案:

答案 0 :(得分:1)

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

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

以这种方式指定cron条目:

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

或者以这种方式指定cron条目:

php -q <path_to_your_script>

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

#!/usr/bin/php

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

discussion

答案 1 :(得分:0)

尝试让cron作业使用wget或curl来点击URL