Laravel 5.3 scheduler doesn't run automatically - php

I'm using Laravel 5.3.26 and cannot set the scheduler to run automatically although i have the cron job ready.
I' ve created a new command ligmena:update, below is the code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class ligmena extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'ligmena:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
//
$today = strtotime("now");
$energa_symvolaia = DB::table('symvolaia')->where('eidos_kinisis', '1')->get();
foreach ($energa_symvolaia as $es) {
$imerominia_lixis = strtotime(str_replace("/", "-", $es->imerominia_lixis));
if ($today > $imerominia_lixis)
DB::table('symvolaia')->where('id', '<', $es->id)->update(['eidos_kinisis' => '4']);
}
}
}
?>
Below is the code of Kernel.php
<?php
namespace App\Console;
use DB;
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\ligmena::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('ligmena:update')->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands() {
require base_path('routes/console.php');
}
}
?>
I've setup the cron job like this:
php /home/site.com/public_html/testdemo/artisan schedule:run >> /dev/null 2>&1
and it runs every minute.
If I run the command manually it works fine.
Any suggestions?

I 've solved the issue using raw mysql.
The cron job was running fine and the code was ok but it was not changing the DB.
After i changed to raw mysql it worked fine

Related

while run cron with lumen : There are no commands defined in the "insert" namespace.

I have written code for cron command as below in lumen (micro framwork of laravel)
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\EmailDump;
use DB;
/**
* dumpEmails Class
*
* This cron is to dump emails with cron use
*
* #author Hetal Gohel <hetal.gohel#brainvire.com>
*
*/
class dumpEmails extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'insert:emails';
/**
* The console command description.
*
* #var string
*/
protected $description = 'This cron is to dump emails with cron use';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
echo "1";die;
}
}
in kernel file defined as below,
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\Laravelista\LumenVendorPublish\VendorPublishCommand::class,
'\App\Console\Commands\dumpEmails',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
// protected function schedule(Schedule $schedule)
// {
// return $schedule;
// }
}
I have fired command from console as below,
php artisan insert:emails
while run this command getting error as below,
[Symfony\Component\Console\Exception\CommandNotFoundException] ←[39;49m
←[37;41m There are no commands defined in the "insert" namespace.
please help me to resolve this issue.thank you.
Please remove __construct and keep just the handle method.
Also, when you are listing under $commands at Kernel you need to specify the class.
So your
`\App\Console\Commands\dumpEmails`
becomes
DumpEmails::class
A few extra tips:
Class names are capitalised; (dumpEmails -> DumpEmails)
Add the { to a new line;
This
class dumpEmails extends Command {
should be
class DumpEmails extends Command
{
I also recommend checking out about PSR-x standards. I left one of the blogs I think might help you kick start with them, but go further! :)
Last, but not least, do not forget Command Parent already lets you use its command line potential. So if you wish to output and debug, you can do so by using:
$this->info('Your message to inform');
$this->error('Your error message');

laravel cache()-get() not working in commands

i have created following command in laravel using command php artisan make:command RedisDataListener
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RedisDataListener extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'listen:subscribe';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Subscribe to redis channel for listening events';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$value = cache()->has('tracking_application:mykey') ? cache()->get('tracking_application:mykey') : 'no';
print_r(array(
$value
));
}
}
but when i'm using cache()->get('key') it is not working. Same function works fine when i use in controller.
i'm using redis as the cache server.
also tried with cache::get() but nothing happen.

Laravel Too many arguments, expected arguments "command" while scheduling

This should be straigh forward buti don't know why it is not working . I am creating a command in laravel to send birtday email reminders on a user's birtday .
Everything works fine and the schedule function is triggered but comes with an error
[Symfony\Component\Console\Exception\RuntimeException]
Too many arguments, expected arguments "command".
This is my command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\User;
class SendBirthdayReminderEmail extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'email:birthday';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Email users a birthday Reminder message';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$users = User::whereMonth('dob', '=', date('m'))->whereDay('dob', '=', date('d'))->get();
foreach($users as $user) {
Mail::queue('emails.birthday', ['user' => $user], function ($mail) use ($user) {
$mail->to($user['email'])
->from('info#XXXXXX.com', 'Company')
->subject('Happy Birthday!');
});
}
$this->info('Birthday messages sent successfully!');
}
}
And this is my kernel.php file
<?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 = [
Commands\SendBirthdayReminderEmail::class
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('email:birthday')->dailyAt('13:00')->timezone('Africa/Dar_es_Salaam');
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
Any help will be appreciated . Thanks :-)
I found a solution,
/opt/php70/bin/php /home/sitename/public_html/artisan schedule:run >/dev/null 2>&1
initially i had 1 after schedule:run method . As below
/opt/php70/bin/php /home/sitename/public_html/artisan schedule:run 1 >/dev/null 2>&1
Your code all looks good.
Have you tried simply as below
php artisan schedule:run
after reaching at your root folder path.

php artisan schedule:run don't send emails

I am trying to send emails from an scheduled laravel task, when I call the command from the application the email is sent, but when the command is called from the command line, it is executed but there is no email sent.
my command code is the next :
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'emails:send';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
try {
Mail::send('emails.testmail', [ ], function ($m) {
$m->to('someona#gmail.com', 'Francisco Larios')->subject('Your Reminder!');
});
} catch (\Exception $e) {
throw new \Exception("Error Processing Request", 1);
}
}
}
the code in the kernel file is the next:
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 = [
Commands\Inspire::class,
Commands\SendEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
//{
// $schedule->command('inspire')->everyMinute();
//}
}
the command I am running on the console is :
php artisan emails:send
the problem was the smtp server I was using to send the emails, I just change it for another smtp server in my mail.php connfiguration file an so, It works for both application and command line execution.

laravel 5 schedule works with ->cron('* */1 * * *') but doesn't work with ->hourly()

I create a command conttroller, this is my code
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Http\Controllers\AppController;
class UpdData extends Command {
protected $name = 'upd:data';
protected $description = 'Update data';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the command.
*
* #return void
*/
public function handle()
{
$app = new AppController;
$this->info("updated!");
$app->update_data();
}
}
This is my crontab
#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
My Kernel.php file
<?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\UpdData',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('upd:data')->hourly(); // case 1
$schedule->command('upd:data')->cron('* */1 * * *'); // case 2
}
}
On the case 1, I try to php artisan schedule:run, I get No scheduled commands are ready to run. message. On the case 2 it work, but I need to command by myself. But the two case doesn't not auto run it. I build my platform on the cloud9, I need your help thanks!
I don't find the reason, but this code is work for me, if I find the real reason, I will update my answer.
This is work for me, replace hourly with cron.
protected function schedule(Schedule $schedule)
{
$schedule->command('upd:data')->cron('0 * * * *');
}

Categories