Laravel 任务计划程序服务器无法在 Windows Server Xampp 上运行 [英] Laravel Task Scheduler Server not working on Windows Server Xampp

查看:21
本文介绍了Laravel 任务计划程序服务器无法在 Windows Server Xampp 上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 任务计划程序在 Windows XAMPP 服务器中无法正常工作.我制作数据库自动备份脚本源.备份数据库工作正常,但 Laravel 调度程序在 Windows 服务器中无法正常工作

<块引用>

app/Console/Commands/DatabaseBackUp.php 使用 php artisan make:command DatabaseBackUp 命令创建的文件

format('Y-m-d') ..sql";$command =".env('DUMP_COMMAND_PATH') .";--用户=".env('DB_USERNAME') .";--密码=".环境('DB_PASSWORD')."--主机=".环境('DB_HOST')."".环境('DB_DATABASE').">".存储路径()./应用程序/备份/".$文件名;$returnVar = 空;$输出=空;执行($命令,$输出,$returnVar);dd("数据库备份成功完成 - $filename Time:- ".Carbon::now());}}?>

<块引用>

app/Console/Kernel.php 文件

command('inspire')->hourly();$schedule->command('数据库:备份')->每天()->appendOutputTo(storage_path('logs/db-backups.log'));}/*** 注册应用程序的命令.** @return 无效*/受保护的功能命令(){$this->load(__DIR__.'/Commands');需要 base_path('routes/console.php');}}

现在当我手动点击 php artisan database:backup 命令时,它会创建数据库备份...

但根据

  1. 根据需要填写输入字段.更多详情请参阅此处如何填写任务调度程序中的字段


对于 Windows(技巧 2)

<块引用>

如果你想防止命令行在调度程序运行你的任务时每分钟都不会弹出.那么你可以使用第二个技巧

  1. 按 Windows + R,写入 Taskschd.msc 并按 Enter &打开任务计划程序

  2. 填写字段.但在操作"选项卡中单击新建",在操作"字段中选择'启动一个程序'

  3. 然后在设置部分填写 C:xamppphpphp.exe 在 Program/Script 输入字段 &C:xampphtdocsmicrotechcouponartisan schedule:run 在添加参数(可选)字段 &然后保存

windows 中第二招的功劳:- https://quantizd.com/how-to-use-laravel-task-scheduler-on-windows-10/

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 AppConsoleCommands;

use IlluminateConsoleCommand;
use CarbonCarbon;

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 AppConsole;

use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'AppConsoleCommandsDatabaseBackUp'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  IlluminateConsoleSchedulingSchedule  $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:xampphtdocslaravel-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)

  1. create .bat file in your applications directory

db-Backup.bat file

write following line in db-Backup.bat file & save it

cd C:xampphtdocsmicrotechcoupon && C:xamppphpphp artisan schedule:run 

2.press Windows + R, write Taskschd.msc and press enter & Task Scheduler will opens, 3.

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

  1. press Windows + R, write Taskschd.msc and press enter & open Task Scheduler

  2. fill the fields. but In the tab 'Actions' click on 'New', in the field 'Action' select 'Start a program'

  3. then in settings section fill C:xamppphpphp.exe in Program/Script input field & C:xampphtdocsmicrotechcouponartisan 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/

这篇关于Laravel 任务计划程序服务器无法在 Windows Server Xampp 上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆