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 usingphp 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)
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.
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/