cronjob无法运行php文件

时间:2015-04-21 09:00:41

标签: php crontab

我想在特定时间后在word新闻网站上发布相同的数据,但我有一个问题:

cronjob无法运行php文件

命令:

* * * * * /usr/bin/php /var/www/html/news/cronpost.php

我的代码:

require 'config/config.php';
require 'config/autoload.php';
require 'IXR_Library.php';

$post = post::getInstance();
$user_websites = user_websites::getInstance();
$user_websites_posts = user_websites_posts::getInstance();
$spintext = spintext::getInstance();

$result = $user_websites->getuserWebsites();

foreach ($result as $row) {
    $minutes = floor((time() - strtotime($row['last_exe_time'])) / 60);
//echo $minutes . "<br>";

    if ($minutes >= $row['schedule_time']) {

        $result2 = $user_websites_posts->getPost($row['id']);

        $id = array();

        foreach ($result2 as $row2) {
            $id[] = $row2['post_id'];
        }

        $ids = implode(",", $id);

//echo $ids . "<br>";

        $row3 = $post->getRandPostId($row['local_cat_id'], $ids);

//echo $row3['id'] . "<br><br>";

        $user_websites->updateuserwebsites($row['id']);

        $user_websites_posts->addUserWebsitesPosts($row['id'], $row3['id']);

        if ($row['spin_id'] == '1') {

            $title = $spintext->api(stripslashes($row3['title']), 'Regular', 'azhar@techpointmedia.com', 'autoblogging', 'true');

            $description = $spintext->api(stripslashes($row3['description']), 'Regular', 'azhar@techpointmedia.com', 'autoblogging', 'true');

        } elseif ($row['spin_id'] == '2') {

            $title = "test title";

            $description = "test description";

        } elseif ($row['spin_id'] == '3') {

            $title = "test title";

            $description = "test description";

        } elseif ($row['spin_id'] == '4') {

            $title = "test title";

            $description = "test description";

        } elseif ($row['spin_id'] == '0') {

            $title = $row3['title'];

            $description = $row3['description'];

        }

        if (isset($row3['image']) && $row3['image'] != "") {

            $xmlrpc = $row['url'] . 'xmlrpc.php';
            $client = new IXR_Client($xmlrpc);

//$client->debug = true;

            $content = array(
                'post_status' => $row['draft_post'],
                'post_type' => 'post',
                'post_title' => $title,
                'post_content' => $description,
                'terms' => array('category' => array($row['wordpress_cat_id'])),
            );
            $params = array(0, $row['username'], $row['password'], $content);
            $client->query('wp.newPost', $params);
            $post_id = $client->getResponse();

            $content = array(
                'name' => basename($row3['image']),
                'type' => $spintext->mime_content_type($row3['image']),
                'bits' => new IXR_Base64(file_get_contents($row3['image'])),
                true,
            );
            $client->query('metaWeblog.newMediaObject', 1, $row['username'], $row['password'], $content);
            $media = $client->getResponse();
            $content = array(
                'post_status' => $row['draft_post'],
                'mt_keywords' => 'tag1, tag2, tag3',
                'wp_post_thumbnail' => $media['id'],
            );
            $client->query('metaWeblog.editPost', $post_id, $row['username'], $row['password'], $content, true);
        } else {
            $xmlrpc = $row['url'] . 'xmlrpc.php';
            $client = new IXR_Client($xmlrpc);

//$client->debug = true; //optional but useful

            $params = array(
                'post_type' => 'post',
                'post_status' => $row['draft_post'],
                'post_title' => $title,
                'post_content' => $description,
//'post_excerpt' => 'This is my test excerpt',
                'post_author' => $row['author_id'],
                'terms' => array('category' => array($row['wordpress_cat_id'])),
            );

            $res = $client->query('wp.newPost', 1, $row['username'], $row['password'], $params);
        }

    }

}

如果我用这段代码替换上面的代码,cronjob运行它:

class DBConnect {
function Connect() {
$root = "root";
$pass = "12345";
return $db = new PDO('mysql:host=localhost;dbname=testing', $root, $pass);
}
}

class operations {

// for single instance
protected static $instance = null;
protected $pdo = null;

// Constructor : database connection
public function __construct() {

try {
$con = new DBConnect();
$this->pdo = $con->Connect();
//echo "connected";
} catch (PDOException $e) {
echo $e->getMessage();
}
}

// Getting single instance
public static function getInstance() {
if (!isset(static::$instance)) {
static::$instance = new static;
}
return static::$instance;
}

public function insertRecord($tableName, $cols, $values) {

try {
$number_of_rows = 0;

$sql = "INSERT INTO `$tableName`($cols) VALUES($values)";

//echo $sql;

//exit();

$count = $this->pdo->exec($sql);

} catch (PDOException $e) {
echo $e->getMessage();
}

if ($count > 0) {
return true;
} else {
return false;
}
}

public function selectRecords($tableName, $select = '*', $where = '1') {
try {

$sql = "SELECT $select FROM $tableName WHERE $where";
$stmt = $this->pdo->query($sql);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
echo $e->getMessage();
}

return $row;
}

public function updateRecord($tableName, $fields, $where) {

try {
$number_of_rows = 0;

$sql = "UPDATE `$tableName` SET $fields WHERE $where";

//echo $sql;
//exit();

$count = $this->pdo->exec($sql);

} catch (PDOException $e) {
echo $e->getMessage();
}

if ($count > 0) {
return true;
} else {
return false;
}
}

}

$show = operations::getInstance();

$result = $show->selectRecords("test2");

//$post->insertRecord("test", "message", "'new message'");
//echo "abc";

foreach ($result as $row) {
echo $row['message'] . "<br>";
$minutes = round(abs(time() - strtotime($row['last_exe_time'])) / 60, 0);
echo $minutes . " min<br><br>";

if ($minutes >= $row['tm'] && $minutes > 0) {

$show->insertRecord("test", "message", "'$row[message]'");

$show->updateRecord("test2", "last_exe_time=CURTIME()", "id=$row[id]");

}
} 

请告诉我为什么cronjob无法运行我的第一个代码。

1 个答案:

答案 0 :(得分:1)

首先,您尝试运行 /usr/bin/php /var/www/html/news/cronpost.php 在控制台中,如果运行成功,则可以尝试使用shell脚本。 创建一个shell脚本文件。内容为/root/cron.sh的示例/usr/bin/php /var/www/html/news/cronpost.php 然后使用chmod + x作为脚本。 最后你创建一个像这样的cronjob

* * * * * /root/cron.sh