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