Laravel 任务计划程序服务器无法在 Windows 服务器 Xampp 上运行
Laravel Task Scheduler Server not working on Windows Server Xampp
Laravel 任务计划程序在 windows XAMPP 服务器中无法正常工作。我制作数据库自动备份脚本源。备份数据库工作正常,但是 Laravel 调度程序在 windows 服务器
中无法正常工作
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');
}
}
现在,当我手动点击 php artisan database:backup
命令时,它会创建数据库备份...
但是根据 Official Doc. of Laravel 8 当我点击 cd C:\xampp\htdocs\laravel-project && php artisan schedule:run >> /dev/null 2>&1
命令时它显示错误 The system cannot find the path specified.
但是当我点击 php artisan schedule:run
命令时,它只创建一次数据库备份。不要像我们提到的那样重复任务。
在 Laravel 文档中提到了调度程序启动命令 cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
它适用于 Linux crontab 功能,对于 Windows 我们需要使用 Task Scheduler
对于Linux
crontab -e
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
或
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
对于Windows(技巧1)
- 在您的应用程序目录中创建 .bat 文件
db-Backup.bat file
在 db-Backup.bat 文件中写入以下行并保存
cd C:\xampp\htdocs\microtechcoupon && C:\xampp\php\php artisan schedule:run
2.press Windows + R,写入 Taskschd.msc 并按回车键 & 任务计划程序将打开,
3.
- 根据需要填写输入字段。了解更多详情
对于Windows(技巧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
按 Windows + R,输入 Taskschd.msc 并按回车键并打开任务计划程序
填写字段。 但在选项卡 'Actions' 中单击 'New',在字段 'Action' select
'Start a program'
然后在设置部分的 Program/Script 输入字段中填写 C:\xampp\php\php.exe
并在添加参数(可选)字段中填写 C:\xampp\htdocs\microtechcoupon\artisan schedule:run
,然后保存
windows 中第二个技巧的功劳:- https://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/
Laravel 任务计划程序在 windows XAMPP 服务器中无法正常工作。我制作数据库自动备份脚本源。备份数据库工作正常,但是 Laravel 调度程序在 windows 服务器
中无法正常工作
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');
}
}
现在,当我手动点击 php artisan database:backup
命令时,它会创建数据库备份...
但是根据 Official Doc. of Laravel 8 当我点击 cd C:\xampp\htdocs\laravel-project && php artisan schedule:run >> /dev/null 2>&1
命令时它显示错误 The system cannot find the path specified.
但是当我点击 php artisan schedule:run
命令时,它只创建一次数据库备份。不要像我们提到的那样重复任务。
在 Laravel 文档中提到了调度程序启动命令 cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
它适用于 Linux crontab 功能,对于 Windows 我们需要使用 Task Scheduler
对于Linux
crontab -e
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
或
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
对于Windows(技巧1)
- 在您的应用程序目录中创建 .bat 文件
db-Backup.bat file
在 db-Backup.bat 文件中写入以下行并保存
cd C:\xampp\htdocs\microtechcoupon && C:\xampp\php\php artisan schedule:run
2.press Windows + R,写入 Taskschd.msc 并按回车键 & 任务计划程序将打开,
3.
- 根据需要填写输入字段。了解更多详情
对于Windows(技巧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
按 Windows + R,输入 Taskschd.msc 并按回车键并打开任务计划程序
填写字段。 但在选项卡 'Actions' 中单击 'New',在字段 'Action' select 'Start a program'
然后在设置部分的 Program/Script 输入字段中填写
C:\xampp\php\php.exe
并在添加参数(可选)字段中填写C:\xampp\htdocs\microtechcoupon\artisan schedule:run
,然后保存
windows 中第二个技巧的功劳:- https://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/