laravel cron not firing but other crons are firing - laravel

I have a ubuntu 18.04 setup and running laravel 5.6 and i have set up the default scheduler in crontab -e
* * * * * php /var/www/html/laravel/artisan schedule:run >> /var/www/html/crons.txt 2>&1
and this is not running
my Kernel code
<?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\CompletelyReviewInternship',
'App\Console\Commands\MigrateUsers',
'App\Console\Commands\MigrateUsersTwo',
'App\Console\Commands\MigrateUsersThree',
'App\Console\Commands\MigrateUsersFour',
'App\Console\Commands\MigrateUsersFive',
'App\Console\Commands\MigrateUsersSix',
'App\Console\Commands\MigrateUsersSeven',
'App\Console\Commands\MigrateUsersEight',
'App\Console\Commands\MigrateUsersNine',
'App\Console\Commands\MigrateUsersTen'
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('review:internship')->hourly();
$schedule->command('migrate:users')->dailyAt('17:40');
$schedule->command('migrate:userstwo')->dailyAt('17:40');
$schedule->command('migrate:usersthree')->dailyAt('17:40');
$schedule->command('migrate:usersthree')->dailyAt('17:40');
$schedule->command('migrate:usersfour')->dailyAt('17:40');
$schedule->command('migrate:usersfive')->dailyAt('17:40');
$schedule->command('migrate:userssix')->dailyAt('17:40');
$schedule->command('migrate:usersseven')->dailyAt('17:40');
$schedule->command('migrate:userseight')->dailyAt('17:40');
$schedule->command('migrate:usersnine')->dailyAt('17:40');
$schedule->command('migrate:usersten')->dailyAt('17:40');
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Please Note :- my commands are firing with php artisan
and i am using nginx as my web server
any advise on this will be advisable

I think there might be multiple reasons of that:
Your application is in maintenance mode. In such case no command will be running unless you use ->evenInMaintenanceMode() for each command
You are using time, but time on your machine is different than what you expect so command will be run at some later point. Make sure time is really valid, or try to change time constraints to ->everyMinute() just to make sure it's not a problem in this case
As a side not - it' not necessary to list all command in $commands property if they are in Commands directory.

if you are using nginx then u have set user www-data in cron tab with super user permission like this
* * * * * sudo -u www-data php /var/www/html/laravel/artisan schedule:run >> /dev/null 2>&1

Related

Laravel Schedule Only Exceute Command on first Run

I create a schedule to send email everyday, but for testing if it works i make it run every minute.
here is my kernel.php
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\SendExpiredReminder::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('reminder:send')
->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
it run every minute but reminder:send only called first time i run the schedule with php /var/www/html/artisan schedule:run >> /dev/null 2>&1
when i run php /var/www/html/artisan schedule:list it show that next due is next minute indicated the scheduler is running. What is going wrong?
your crontab should be to your laravel project folder, not www folder
* * * * * cd /var/www/html/artisan && php artisan schedule:run >> /dev/null 2>&1
note the project folder

How to let cron decide the time to run laravel schedule daily?

I don't want to set server cron to run every minute (* * * * *) so I set it to #daily
#daily usr/bin/php /home/dss/laravelAppDss/artisan schedule:run >> /home/dss/public_html/example.txt
This is my 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\SinkronDataDosen',
'\App\Console\Commands\SinkronDataMahasiswa',
'\App\Console\Commands\SinkronPotensiDO',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('SinkronDataDosen');
$schedule->command('SinkronDataMahasiswa');
$schedule->command('SinkronPotensiDO');
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
But it doesn't sync the data
This is the output example.txt
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronDataDosen > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronDataMahasiswa > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronPotensiDO > '/dev/null' 2>&1
The commands worked if I run each command signature individually so there is nothing wrong with the commands
How do I solve it?

why cron job stoped in new year 2021 in laravel

every thing works fine till 31 decemeber 2020 and in new year my cron job stoped working i dont know what happend i am using spatie laravel package to take db backup an i also have one other cron job in console and command
kernal.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\UpdateUserNotNew',
];
/**
* 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('cron:update-user-not-new')->everyMinute();
$schedule->command('backup:run')->everyMinute();
$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');
}
}`
my issue is resolved byhttps://stackoverflow.com/a/65890784/14913109
only my php version was not integrated with php multimanager
The problem is most likely not coming from spatie/laravel-backup package. To run Laravels cronjob you need to a have a central call in your crontab to Laravels scheduler
* * * * * cd /path-to-your-project && php artisan schedule:run
You can also check all the scheduled tasks locally by running
$ php artisan schedule:work
and if they run fine locally then there is an issue with your server, but not with Laravel.
Also always have a look at storage/logs/laravel.log if there are any errors showing up.

Laravel cron job runs in localhost but not in my shared hosting with cpanel

I've to send automated emails to members of my subscription system. When I schedule a job in my laravel project and cron job on my localhost it runs properly every minute. But when I upload the same project to my shared hosting the scheduled task doesn't run. Can anyone help me fix this issue?
My CronTab:
SHELL="/bin/bash"
* * * * * php /home/tradiet1/public_html/tradiedemo/tradiepackage/artisan schdule:run
My NotifyMembershipRenewal:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Jobs\SendMembershipRenewalNotification;
use App\AppLog;
use Carbon\Carbon;
use App\User;
class NotifyMembershipRenewal extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'notify:membership_renewal';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send notification email to users informing them their membership subscription is about to expire .';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$args = request()->server('argv');
$log = new AppLog;
$log->url = url()->current();
$log->details = "Cron started at: ".date('Y-m-d H:i:s')." in dir: ".__DIR__;
$log->details .= "\r\n".' args: '.json_encode($args);
\DB::enableQueryLog();
$now = Carbon::now();
$expiryInDays = 7;
$from = $now->addDays($expiryInDays-1)->format('Y-m-d H:i:s');
$to = $now->addDay()->format('Y-m-d H:i:s');
$users = User::with('membership')
->where('role', 3)
->whereBetween('membership_expiry', [$from, $to])
->get();
foreach($users as $key => $user)
{
dispatch(new SendMembershipRenewalNotification($user, $expiryInDays));
}
$log->details .= "\r\n Count: ".$users->count();
$log->details .= "\r\n ".json_encode(\DB::getQueryLog())."\r\n \r\n";
$log->save();
$this->info($log->details);
}
}
My Kernel.php:
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\AppLog;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$log = new AppLog;
$log->details = "Inside Kernel#schedule";
$log->save();
$schedule->command('notify:membership_renewal')
->everyMinute() // 22:00 UTC = 08:00 AEST
->appendOutputTo('./app_log.txt')
->emailOutputTo(env('APP_LOG_EMAIL'));
/*$schedule->command('inspire')
->hourly();*/
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
My Crontab for localhost:
SHELL="/bin/bash"
* * * * * php /var/www/html/is/tradiedemo/tradiepackage/artisan schedule:run
My Crontab for my live shared hosting server
SHELL="/bin/bash"
* * * * * php /home/tradiet1/public_html/tradiedemo/tradiepackage/artisan schdule:run
The cron jobs run normally on my localhost and I get email every minute and log is stored in the DB as well as file. But when I transfer the project to my server the cron job runs every minute but the scheduled task doesn't run every minute. The task doesn't run even once.
In Shared Hosting you can't run a cron job every minute. because of it's a limitation on shared hosting(In Godaddy or Hostgator I guess). so you can do like run every 30 min every 15 min.
You read it about here too Cron Job in hostgator
But ya some Hosting provider provide this facility too.
So now the question is why it's not running right.
Its happened with me too. there is some issue with register_argc_argv but sometimes setting it on also not work so best practice rather than do it with PHP.
You can do this with pho-cli that's it.
Definitely it will work.
Do it like below
SHELL="/bin/bash"
* * * * * php-cli -q /home/tradiet1/public_html/tradiedemo/tradiepackage/artisan schdule:run
Hope it works for you.
Update
So as you said it does not work for you. so there is a rough solution for this is.
why not you do a cron on URL which is quite easy and work on any shared hosting and Cpanel things.
so for this, you can use wget for url. Below is the code related to it.
wget http://example.com/check
and now on your route function run your artisan programmatically something like below.
Route::get('/check', function () {
Artisan::call('schedule:run');
});

Laravel 5.4 - schedule:run works only once

I made a custom artisan command called db:dump which dumps my database into a sql file.
I tried applying it into a scheduler so it'll backup every time depending on the time range I want to apply. In this case, I tested it with everyMinute()
Here's what my Kernel look's like.
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\MySqlDump::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('db:dump')
->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
It only works once when I execute php artisan schedule:run in my command prompt. Tried waiting for 5 minutes to see if it will backup 5 times but it didn't. I tried running schedule:run repeatedly and that's when it only worked each time. Basically speaking, it dumps an sql file every time I run schedule:run.
I'm totally lost now, advanced thanks to those who can help me. :)
You need to run cronjob on control panel plan servers like Plesk or directadmin or Cpanel.
egg:visit
If you are running locally, then you should use the following.
php artisan schedule:work
When you are on a live server, you have to set the cron configuration like the following
https://laravel.com/docs/8.x/scheduling#running-the-scheduler-locally

Resources