简体   繁体   中英

Laravel cron job is not running

This is my first time setting up Cron job and I cannot make it to automatically schedule a command.

So I have a command:

public function handle()
{
    $client = new Client();
    $crawler = $client->request('GET', 'http://www.oldham-chronicle.co.uk/news-features');
    $crawler->filter('div[id=content]>.homefeature')->each(function ($node, $key) {
        $title = $node->filter('.plain')->text();
        $datepublished = $node->filter('.dateonline')->text();
        $description = $node->filter('.teaser-link')->text();
        $link = $node->filter('a')->link();
        $link_r = $link->getUri();
        $image = $node->filter('img')->image();
        $image_s = $image->getUri();
        $filename = basename($image_s);
        $image_path = ('news-gallery/' . $filename);
        Image::make($image_s)->save(public_path('news-gallery/' . $filename));
        $id = 1+ $key + 1;
        $news = News::where('id', $id)->first();
        // if news is null
        if (!$news) {
            $news = new News();
        }
        $news->title = $title;
        $news->datepublished = $datepublished;
        $news->description = $description;
        $news->link = $link_r;
        $news->image = $image_path;
        $news->save();
    });

    $crawler->filter('div[id=content]>.teaser-50')->each(function ($node, $key) {
        $title = $node->filter('.plain')->text();
        $datepublished = $node->filter('.dateonline')->text();
        $description = $node->filter('.teaser-link')->text();
        $link = $node->filter('a')->link();
        $link_r = $link->getUri();
        $image = $node->filter('img')->image();
        $image_s = $image->getUri();
        $filename = basename($image_s);
        $image_path = ('news-gallery/' . $filename);
        Image::make($image_s)->save(public_path('news-gallery/' . $filename));
        $id = 1+ $key + 1;
        $news = News::where('id', $id)->first();
        // if news is null
        if (!$news) {
            $news = new News();
        }
        $news->title = $title;
        $news->datepublished = $datepublished;
        $news->description = $description;
        $news->link = $link_r;
        $news->image = $image_path;
        $news->save();
        $this->info('Scraping done succesfully');
    });
}

Kernel file:

protected $commands = [
    'App\Console\Commands\NewsScrape'
];

/**
 * Define the application's command schedule.
 *
 * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
 * @return void
 */
protected function schedule(Schedule $schedule)
{
    $schedule->command('scrape:news')
             ->everyMinutes();
             ->emailOutputTo('some@email);
}

I can manually run it as php artisan scrape:news and it works, however when I add it to crontab -e:

* * * * * php /var/www/fyproject/ schedule:run >> /dev/null 2>&1

Cronjob never runs, any help!

The command needs to be invoked via artisan so you must remember to add it in the call:

Use:

crontab -e * * * * * php /var/www/fyproject/artisan schedule:run >> /dev/null 2>&1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM