Ubuntu - Не запускается PHP скрипт по крону

at0m1x

Новичок
Сделал PHP скрипт, который нужно выполнять по крону. Но почему то он не запускается...

user@user-computer:~$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

#*/1 * * * * /usr/bin/notify-send "test" "test" -i skype
#*/10 * * * * /usr/bin/php -f /home/user/www/test/www/test.php

*/10 * * * * /usr/bin/php5 -q /home/user/www/test/www/test.php


Пробовал запускать из под консоли /usr/bin/php5 -q /home/user/www/test/www/test.php
Работает, по крону не работает.

Нужно что бы каждые 10 мин запускался.
 

zerkms

TDD infected
Команда форума
В конце последней строки есть перевод строки?
 

at0m1x

Новичок
Проверил крон, работает, в mysql базу записываются данные.

Код моего скрипта:

PHP:
<?php

mysql_connect('127.0.0.1', 'root');
mysql_select_db('rss');
mysql_query('SET NAMES utf8');

require (dirname(__FILE__) . '/phpQuery/phpQuery.php');

$habrablog = file_get_contents('http://habrahabr.ru/blogs/');
$document = phpQuery::newDocument($habrablog);
$posts = $document->find('div.posts_list > div.posts div.post');
foreach ($posts as $post) {
    $pq = pq($post);

    $blog_title = $pq->find('a.blog_title')->text();
    $blog_title = trim($blog_title);
    
    $post_title = $pq->find('a.post_title')->text();
    $post_title = trim($post_title);
    
    $post_link = $pq->find('a.post_title')->attr('href');

    $text = $pq->find('div.content')->text();
    $text = trim($text);
    $text = mb_substr($text, 0, 255, 'UTF-8') . '...';
    
    $query = "
        SELECT `id`
        FROM `posts`
        WHERE `link` = '" . mysql_real_escape_string($post_link) . "'
        ";
    if(mysql_num_rows(mysql_query($query)) == 0) {
        $query = "
            INSERT INTO `posts`
            SET
                `link` = '" . mysql_real_escape_string($post_link) . "',
                `title` = '" . mysql_real_escape_string($blog_title . ' -> ' . $post_title) . "',
                `description` = '" . mysql_real_escape_string($text) . "',
                `date` = NOW()
            ";
        mysql_query($query);

        $command = '/usr/bin/notify-send "' . $blog_title . ' -> ' . $post_title . '" "' . $text . '" -i ' . dirname(__FILE__) . '/rss.png';
        //system($command);
        //exec($command);
        shell_exec($command);

        break;
    }
}
Этот скрипт должен получать последние посты с хабра, записывать их в базу и выводить нотификацию с помощью notify-send. Если запускать с консоли (из под обычного юзера, не из под рута), все работает. А по крону, только в базу записывает, нотификацию не выводит... ОС Ubuntu

Из-за чего может не выводить нотификацию?
 

Dovg

Продвинутый новичок
Крон работает от того же пользователя, что и "консоль" ?
 

Dovg

Продвинутый новичок
Судя по симптомам, он у тебя DISPLAY не может найти.

*/1 * * * * DISPLAY=:0.0 notify-send test
- вот так будет работать. Без определения DISPLAY - нет.

вообще, хардкодить DISPLAY не хорошо
 
Сверху