cron job with laravel to send emails to user - laravel

please help me i'm trying to make a cron job on my server to send email to user
i created a command like that :
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'contract:end';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send an email to user about the end of a contract';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$contracts = Contract::where('end_date', Carbon::parse(today())->toDateString())->get();
foreach ($contracts as $contract) {
Mail::to(Setting::first()->value('email'))->send(new ContractEmail($contract));
}
}
and in my kernel.php i added the following code :
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
Commands\ContractEnd::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('contract:end')
->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
and on my server i run the following command to run the cron job :
cd /archive/artisan && schedule:run >> /dev/null 2>&1
but it didn't send any email , can anyone help me as it's my first time using it ?

Because your command in your server is wrong you need to specify the correct path which will contain/home/your username/your website folder/artisan like this
/usr/local/bin/php /home/your user name which you can get in from job page/archive/artisan schedule:run 1>> /dev/null 2>&1

Related

No scheduled commands are ready to run. Laravel 8

I'm trying to run command every minute with scheduler but scheduler:run prints No scheduled commands are ready to run. message. As I understand, I don't need to set cronjob if I want to test shcedulder by run command. Any suggestions how to solve this issue are appreciated
My command:
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'print:log';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Print in log';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
info('message from command');
}
Kernel.php
protected $commands = [
'App\Console\Commands\PrintLog',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('print:log')->everyMinute();
}
Can you try as?
protected $commands = [
Commands\PrintLog::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('print:log')->everyMinute();
}
You misunderstand. Read docs here.
schedule:run command is for production - you must use it with cronjob on server
schedule:work command is for local development and testing. Run this command in terminal window and wait one minute - your scheduled job will be dispatched.

Laravel Scheduling not firing command

I have a simple set up a command for Scheduling that is firing on a cron call
The artisan command works fine yet nothing happens when Scheduling
The Schedule
protected function schedule(Schedule $schedule)
{
$schedule->command('prices:pricing')
->everyMinute();
}
The Task
protected $signature = 'prices:pricing';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Gets Pricing data and stores in DB';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
Log::debug('An informational message.');
}
}
The cron
* * * * * www-data cd /var/www/api && php artisan schedule:run >> /dev/null 2>&1
Also I have tried with running
php artisan queue:listen

my cron job not working well in laravel it work only once

i am working on laravel crone job it work only one time my code execute only one time i am a beginer
protected function schedule(Schedule $schedule)
{
$schedule->command('list:users')->everyMinute();
}
class ListUsers extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'list:users';
/**
* The console command description.
*
* #var string
*/
protected $description = 'corn job command';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$totalUsers = \DB::table('users')->whereMonth('created_at','04')->count();
Mail::to('qasim#gmail.com')->send(new SendMailable($totalUsers));
}
I am also try this code
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
echo "pakistan\n";
$totalUsers = \DB::table('users')->whereMonth('created_at','04')->count();
Mail::to('qasim#gmail.com')->send(new SendMailable($totalUsers));
echo "pakistan";
})->everyMinute();
}
i want user recieve email after every minute
i recieve mail once but not every minute

Laravel 5.0 : Task scheduling

I am using Laravel 5.0.*, Now I want to implement cron job in my application.
I searched a lot but I get nothing, all the example and video tutorials are related to 5.1 and 5.3 even I search in laravel.com for task scheduling under 5.0 version but it showing nothing
Reference video link : video link
And after using above video reference, I am getting below error in terminal.
[2016-11-02 08:47:06] local.ERROR: exception 'InvalidArgumentException' with message 'There are no commands defined in the "cron" namespace.' in D:\harendar\htdocs\nkbuild\vendor\symfony\console\Symfony\Component\Console\Application.php:501
/app/Console/Kernel.php
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\LogDemo',
];
/**
* 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('log:demo')->emailOutputTo('harendar#solutionavenues.com');
}
}
/app/Console/Commands/LogDemo.php
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class LogDemo extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'log:demo';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Log Demo command.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
\log::info('I was here # ', \Carbon\Carbon::now());
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return [
['example', InputArgument::REQUIRED, 'An example argument.'],
];
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return [
['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
}
Hello First you need to run command : php artisan make:console test(command name).
Then you find one test.php command created under app/Console/Commands/test.php .this looks like:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class test extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'test';
/**
* 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()
{
//
}
}

getting error like:- [Symfony\Component\Console\Exception\RuntimeException] Too many arguments

This is my UserCreateCommand.php file when command:- php artisan user-create username password getting error like:- [Symfony\Component\Console\Exception\RuntimeException] Too many arguments.
class UserCreateCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'user-create';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create user with password imediately';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
$username=$this->argument('username');
$password=$this->argument('password');
$user= new UserDemo();
$user->username=$username;
$user->password=Hash::make($password);
$user->save();
$this->info('user has been cretaed');
}
}
public function handle()
{
//
$username=$this->ask('username');
$password=$this->secret('password');
$user= new UserDemo();
$user->username=$username;
$user->password=Hash::make($password);
$user->save();
$this->info('user has been cretaed');
}
working fine when used command:- php artisan user-create

Resources