Same problem schedule cron job,
my kernal.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Log;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
// \App\Console\Commands\SecondTable::class,
];
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
//->hourly();
$schedule->call('App\Http\Controllers\Sale\SaleController#sync')->everyMinute();
}
protected function commands()
{
require base_path('routes/console.php');
}
}
manually running a command-> php artisan schedule:run is working good!
but, cronjob run server is not working correctly,
my cron job code,
* * * * * php /laravel project folder/artisan schedule:run >> /dev/null 2>&1
is not working.
Check your cronjob code, it could be something like this:
* * * * * cd path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
It means: every seconds (* * * * *), go to my project folder (cd path-to-your-project), then execute the command (php artisan schedule:run).
The meaning of the last part of the cronjob is explained here: What is /dev/null 2>&1?
Related
as Running The Scheduler says I must run this Command:
* * * * * cd /home/u285707107/domains/dmn.com/ && php artisan schedule:run >> /dev/null 2>&1
but in hPanel Cron Job-s I can't use special characters, so I create cron.sh file as they say
#!/bin/sh
/home/u285707107/domains/dmn.com/ && php artisan schedule:run > /dev/null 2>&1
and in Hostinger Cron Job I run Command:
/bin/sh /home/u00000000/domains/dmn.com/public_html/dmn/app/Console/cron.sh
but it don't works : ( (
My Kernel.php file is:
<?php
namespace App\Console;
use App\Models\Proview;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule){
$schedule->call(function () {
Proview::query()->update(['today' => 0]);
})->everyMinute();
}
protected function commands(){
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
can someone Help me ?
Just replace the below scripts with yours.
cron.sh
#!/bin/sh
cd /home/u285707107/domains/dmn.com && php artisan schedule:run >> /dev/null 2>&1
Hostinger Cron Job
* * * * * /bin/sh /home/u285707107/domains/dmn.com/public_html/dmn/app/Console/cron.sh
Laravel Task Scheduler not works properly in windows XAMPP server. I make Database Auto backup script source. backup database is works fine but, Laravel scheduler not works properly in windows server
app/Console/Commands/DatabaseBackUp.php file created using php artisan make:command DatabaseBackUp command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
class DatabaseBackUp extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'database:backup';
/**
* 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 int
*/
public function handle()
{
$filename = "backup-" . Carbon::now()->format('Y-m-d') . ".sql";
$command = "". env('DUMP_COMMAND_PATH') ." --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
dd("Database backup Successfully Done - $filename Time:- ".Carbon::now());
}
}
?>
app/Console/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\DatabaseBackUp'
];
/**
* 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('database:backup')
->daily()
->appendOutputTo(storage_path('logs/db-backups.log'));
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Now when I hit php artisan database:backup command manually then it is create Database backup...
but as per Official Doc. of Laravel 8 when I hit cd C:\xampp\htdocs\laravel-project && php artisan schedule:run >> /dev/null 2>&1 command it shows error like The system cannot find the path specified.
but when I hit the php artisan schedule:run command then it create database backup only once. not repeat task as we mentioned.
in Laravel Docs there are mentioned scheduler start command cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 it is works for Linux crontab Feature, for Windows we need to use Task Scheduler
for Linux
crontab -e
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
OR
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
For Windows (Trick 1)
create .bat file in your applications directory
db-Backup.bat file
write following line in db-Backup.bat file & save it
cd C:\xampp\htdocs\microtechcoupon && C:\xampp\php\php artisan schedule:run
2.press Windows + R, write Taskschd.msc and press enter & Task Scheduler will opens,
3.
fill the inputs fields as per need. for more detail see here how to fill fields in Task scheduler
For Windows (Trick 2)
If you wants to prevent command line won’t popup every single minute when scheduler runs your task. then you can use 2nd trick
press Windows + R, write Taskschd.msc and press enter & open Task Scheduler
fill the fields. but In the tab 'Actions' click on 'New', in the field 'Action' select
'Start a program'
then in settings section fill C:\xampp\php\php.exe in Program/Script input field & C:\xampp\htdocs\microtechcoupon\artisan schedule:run in Add arguments (optional) field & then save it
credit of 2nd Trick in windows :- https://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/
I'm trying to run backup using cron, i'm using spatie-laravel-backup it is working when i run it manually but when i run it using cron using this path on server:
/home/user/public_html && php artisan backup:run >> /home/user/logs 2>&1
and i'm getting this
/bin/bash: /home/user/public_html: Is a directory"
kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('10:00');
}
run scheduler from cron
https://laravel.com/docs/8.x/scheduling#running-the-scheduler
* * * * * cd /home/user/public_html && php artisan schedule:run >> /dev/null 2>&1
then
you can use
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('10:00');
}
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.
I have made a scheduler. When I call it with php artisan userRanking it works.
This is the code in Kernel.php:
protected $commands = [
\App\Console\Commands\UserRanking::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('userRanking')
->everyMinute();
}
How do I dispatch it so that it runs automatically?
You have to set up a single cron job that calls the schedule runner every minute:
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
Read Laravel's Scheduler docs for more info.