How to use Laravel 5.1 schedule? - php

I'm trying to use laravel 5.1 scheduler to run a data grabbing commands from twitter and store it in the database. If I put the actual command within the Kernel.php file it runs no worries and gets/inserts the data into the database but when I try put it into an external file to run and just put the command in the Kernel I get this
Running scheduled command: (touch/storage/framework/schedule-4ff51352255727a7381f73c7bd3eb90c; '/Applications/MAMP/bin/php/php5.5.18/bin/php' 'artisan' lookup; rm /storage/framework/schedule-4ff51352255727a7381f73c7bd3eb90c) > /dev/null 2>&1 &
This is my Kernal.php file
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use DB;
use Session;
use Carbon\Carbon;
use Thujohn\Twitter\Facades\Twitter;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\Scheduled_post::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('lookup')
->everyMinute()
->withoutOverlapping();
}
}
and my command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
class Lookup extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'Lookup';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Performing user lookup';
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$user_lookup = Twitter::getUsersLookup(['screen_name' => 'some_user', 'format' => 'object']);
$social_user = array(
'email' => null,
'screen_name' => $user_lookup[0]->screen_name,
);
DB::table('social_users')->insert( $social_user );
}
}
Testing in MAMP, manually executing php artisan schedule:run through terminal to test.
Im not sure how to fix this or whats going on?

OK, in your Kernel, use the correct signature, and add the command to your :
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use DB;
use Session;
use Carbon\Carbon;
use Thujohn\Twitter\Facades\Twitter;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\Scheduled_post::class,
\App\Console\Commands\Lookup::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('Lookup')
->everyMinute()
->withoutOverlapping();
}
}
It
It's Lookup not lookup.

Related

I want to run a task schedule at daily and I am trying execute a function which will initialize the value in the database

I want to run a task schedule at daily and I am trying execute a function which will initialize the value in the database.But terminal showing error while running php artisan schedule:work
2022-11-23 06:25:00 Running [Callback] ................................................................................................ 899ms FAIL
The code is
<?php
namespace App\Console;
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->call(function () {
$schedule = schedules::find(1);
$schedule->endTime = "00:00:00";
$schedule->save();
}->daily();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}

Cron job is not working when specify time

I have a Laravel project v8 and I have created a cron job for DB backup
It's working every minute but it is not working when I specify the time for daily.
Project timezone is 'Asia/Kolkata' and my GoDaddy shared server timezone is UTC.
kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('backup:clean')->everyMinute();
$schedule->command('backup:run')->cron('51 3 * * *');
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
my cronjob on Cpanel.
You can run cron like this:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->dailyAt('03:51');
}
Replace your kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:clean')->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
And after this set cronjob on Cpanel with the time on which you want to execute
Check that the given time in cpanel the cron will work definitely

Laravel $schedule to read/send email

I´m trying to create $schedule job in Laravel to read Email with PHP IMAP package. If I go to route, package read email and does all correctly, but need to do this every 5 minutes.
I create a new command class and add this
use Illuminate\Http\Request;
class ReadMail extends Command implements SelfHandling {
protected $name = 'read:mail';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the command.
*
* #return void
*/
public function fire()
{
$request = Request::create($this->option('App\Http\Controllers\MailController#index'), 'GET');
$this->info(app()['Illuminate\Contracts\Http\Kernel']->handle($request));
}
In kernel
protected $commands = [
'App\Console\Commands\ReadMail',
];
protected function schedule(Schedule $schedule)
{
$schedule->call('read:mail')
->everyFiveMinutes();
}
I'm not sure if this code it´is correct, but does not work properly. Any idea about it?
Thank in advance for your help.
UPDATE
I launch this
php artisan read:mail and return
Argument 1 passed to Illuminate\Console\Application::add() must be an instance of Symfony\Component\Console\Command\Command, instance of App\Commands\ReadMail given
The code of ReadMail class
<?php namespace App\Commands;
use App\Commands\Command;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Http\Request;
class ReadMail extends Command implements SelfHandling {
protected $signature = 'read:mail';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the command.
*
* #return void
*/
public function fire()
{
$request = Request::create($this->option('App\Http\Controllers\MailController#index'), 'GET');
$this->info(app()['Illuminate\Contracts\Http\Kernel']->handle($request));
}
}
UPDATE 2: SOLVED - ALL CODE
Kernel
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'App\Commands\ReadMail',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('read:mail')
->everyFiveMinutes();
}
}
ReadMail
<?php namespace App\Commands;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Http\Request;
use Illuminate\Console\Command;
use App\Http\Controllers\MailController;
class ReadMail extends Command implements SelfHandling {
protected $name = 'read:mail';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the command.
*
* #return void
*/
public function handle()
{
MailController::index();
}
}
MailController
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use PhpImap\Mailbox as ImapMailbox;
use App\Models\Escalas;
class MailController extends Controller {
public static function index()
{
$mailbox = new ImapMailbox('{imap.gmail.com:993/imap/ssl}INBOX', '', '','');
$mailsIds = $mailbox->searchMailbox('UNSEEN');
if(!$mailsIds) {
die('Mailbox is empty');
}
$mail=[];
foreach ($mailsIds as $index=>$data){
$mail[]=$mailbox->getMail($mailsIds[$index]);
Escalas::insert([
['' => $mail[$index]->textPlain,
'' => $mail[$index]->date,
''=>$mail[$index]->subject,
''=>$mail[$index]->fromName,
''=>$mail[$index]->fromAddress,
''=>$mail[$index]->toString],
]);
}
}
}
Try changing
protected $name = 'read:mail';
with protected $signature= 'read:mail';
in your ReadMail class and then run in Kernel like this
$schedule->command('read:mail')->everyFiveMinutes();
Check whether your scheduled tasks are set up properly (see docs for Laravel 5.0) Make sure you've added a cronjob to trigger Laravel's scheduled commands. To check you cronjobs are running, look for a file like /logs/crond.log. When opening this file, you should see lines showing at what times this ran. This is the command that triggers Laravel's scheduled jobs.
If all that is correct, then try running your command via your terminal on localhost to check the command is all set. This should reveal any problems with the command setup. Your functionality itself seems allright, since you mentioned everything works when triggered via a route.

Laravel 5.1 - Get artisan arguments from internal call

I do
when I run artisan queue:work or artisan queue:listen it runs the current commands with their corresponding Arguments. Now my question is, how can I Access those Arguments?
As you can see in the following Picture, the Arguments are there but I have no clue how to Access them?
In a project which follow a "standard project structure"
You must have a class in app/Console named Kernel which extends Illuminate\Foundation\Console\Kernel, an example of how to implement it is as follows:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* {#inheritdoc}
*/
protected $commands = [
//here you have to put your commands class
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule): void
{
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands(): void
{
require base_path('routes/console.php');
}
}
so now let's create a new command, call it "print" and it will accept a parameter called text, here is the implementation :
<?
namespace App\Console\Commands;
use Illuminate\Console\Command;
class TestCommand extends Command
{
/**
* {#inheritdoc}
*/
protected $signature = 'test {text}';
/**
* {#inheritdoc}
*/
protected $description = 'Test command.';
/**
* {#inheritdoc}
*/
public function handle()
{
$this->info($this->argument('text'));
}
}
as you can see, the new command accept a parameter called text and print it in console.
So to retrieve the parameter sent to an command call, you have to use the argument method in the follow way:
$commandInstance->argument('key_of_parameter');
To get more info read the docs

I need to install composer on my server to be able to perform a cron job with laravel?

Is my first project in laravel and I am trying to perform a cron job but for some reason this does not seem to work, initially I worked the project in local, then upload it to the server and never installed composer in the cPanel, this is the cause that Does not work ?, I leave the code I have used.
for the command
<?php
namespace App\Console\Commands;
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Plan;
use App\Plan_negocio2;
use App\Plan_negocio;
use App\Pagina;
use DateTime;
use Illuminate\Console\Command;
//use Illuminate\Foundation\Inspiring;
class Resaltador extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'res:resaltado';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Verificar Resaltador';
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$plan=Plan_negocio::all();
//$fechaAct=$request->fecha;
if ($plan)
{
$arrNegocio=[];
foreach ($plan as $key => $value)
{
//$value->fechafin=strtotime("2017-04-07")-time();
date_default_timezone_set('America/Caracas');
$date = new DateTime($value->fechafin);
$dateHoy=new DateTime();
//$dat=$dateHoy->getTimestamp();
$dat=9999999999999999;
$value->fechafin=$date->getTimestamp();
//$now = new DateTime();
//$gene= $date1->format('U') - $now->format('U');
if ($dat>$value->fechafin)
{
$negoPlan=Pagina::find($value->negocio_id);
$negoPlan->resalta_id=1;
$res=$negoPlan->save();
}
}
}
}
}
code of the kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//'App\Console\Commands\Inspire',
\App\Console\Commands\Resaltador::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
/*$schedule->command('inspire')
->hourly();*/
$schedule->command('res:resaltado')->everyMinute();
}
}
route of the cron job
The path that I give to the cron job does not give me any errors, so I do not think this is giving errors
the path is this
/usr/bin/php -q home3/pixsony6/public_html/loupper.com/loupper/artisan schedule:run 1>> /dev/null 2>&1
Any suggestions would be good, thanks
No, you do not need Composer to run a cron job with Laravel.
If you're vendor folder is present with all dependancies then you can run commands.
Composer is for installing packages to the vendor folder.
For your specific problem, try using the php-cli instead of php

Categories