Laravel 任务计划程序在 Windows XAMPP 服务器中无法正常工作。我制作数据库自动备份脚本源。备份数据库工作正常,但 Laravel 调度程序在 Windows 服务器中无法正常工作
app/Console/Commands/DatabaseBackUp.php
使用php artisan make:command DatabaseBackUp
命令创建的文件
<?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
文件
<?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');
}
}
现在,当我php artisan database:backup
手动点击命令时,它会创建数据库备份...
但根据官方文档。当我点击命令时,Laravel 8cd C:\xampp\htdocs\laravel-project && php artisan schedule:run >> /dev/null 2>&1
显示错误,例如The system cannot find the path specified.
但是当我点击php artisan schedule:run
命令时,它只创建一次数据库备份。不要重复我们提到的任务。