Windows 上的 Laravel 5.1 任务调度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32950083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:27:25  来源:igfitidea点击:

Laravel 5.1 Task Scheduling on Windows

laravelbatch-filecronscheduled-taskslaravel-5.1

提问by suncoastkid

I'm trying to get Laravel 5.1 Task Scheduling working on IIS. When I run a batch file using Windows task manager it will run the task one time only. How can I get ->everyMinute() to work?

我正在尝试让 Laravel 5.1 任务调度在 IIS 上工作。当我使用 Windows 任务管理器运行批处理文件时,它只会运行该任务一次。我怎样才能让 ->everyMinute() 工作?

Windows batch file:

Windows 批处理文件:

cd c:\inetpub\myapp
c:\PROGRA~2\PHP\php.exe artisan schedule:run 1>> NUL 2>&1

The kernel:

内核:

class Kernel extends ConsoleKernel
{
    protected $commands = [

        \App\Console\Commands\MyCommand::class,

    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('test')->everyMinute();
    }
}

The command:

命令:

public function handle()
    {
        log::info('test');
    }

回答by bernie

Take a look at the task schedulerdoc.

查看任务调度程序文档。

Starting The Scheduler

Here is the only Cron entry you need to add to your server:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

This Cron will call the Laravel command scheduler every minute. Then, Laravel evaluates your scheduled tasks and runs the tasks that are due.

启动调度程序

这是您需要添加到服务器的唯一 Cron 条目:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

这个 Cron 每分钟都会调用 Laravel 命令调度程序。然后,Laravel 评估您的计划任务并运行到期的任务。

In your case, you use the Windows task scheduler instead of Cron but the important thing is to call artisan schedule:runevery minute. Each time this command is run, it checks its schedule and runs the due tasks/commands added.

在您的情况下,您使用 Windows 任务调度程序而不是 Cron,但重要的是artisan schedule:run每分钟调用一次。每次运行此命令时,它都会检查其计划并运行添加的到期任务/命令。

artisan schedule:rundoes not start a long-running process that stays alive to runs tasks until you kill it. As I said, it needs to be called every minute.

artisan schedule:run不会启动一个长时间运行的进程,该进程在您杀死它之前一直处于活动状态以运行任务。正如我所说,它需要每分钟调用一次。

回答by Dario Corno

You need to create a scheduled task that will execute that batch file every minute.

您需要创建一个计划任务,每分钟执行一次该批处理文件。

To do so :

这样做:

  • Press Win+ Rand run taskschd.msc

  • In the right panel click Create Basic Taskand give it a Name + Description.

  • Click Next and select Start a Programoption, then navigate to the batch file and select it. No need to fill the other fields.

  • Select "Open the properties of this task..." and then Finish.

  • On the Triggertab, you can change between Dailyor At Logon(as I do).

    Here is the part that's not documented, open the dropbox and insert 1using the keyboard, this is the only way to set the repeat time at 1 minute (even if the dropdown doesn't list it).

    Larevelneeds the cronjobto run every minute, otherwise it won't work as expected.

    Also check "Indefinitely" to run it forlifetime.

  • Win+R并运行taskschd.msc

  • 在右侧面板中单击创建基本任务并为其指定名称 + 描述。

  • 单击下一步并选择启动程序选项,然后导航到批处理文件并选择它。无需填写其他字段。

  • 选择“打开此任务的属性...”,然后选择“完成”。

  • Trigger选项卡上,您可以在DailyAt Logon之间切换(就像我一样)。

    这是未记录的部分,打开 Dropbox 并使用键盘插入1,这是将重复时间设置为 1 分钟的唯一方法(即使下拉列表未列出)。

    Larevel需要cronjob每分钟运行一次,否则它将无法按预期工作。

    还要选中“无限期”以使其终生运行。

Hope it helps.

希望能帮助到你。

The Windows Task Scheduler Helpis here, if you run into trouble.

Windows任务计划程序帮助在这里,如果你遇到了麻烦。

回答by Robert Ni?o

I have a single solution Create to file Executable xxx.cmd, Open the file and write the next text.

我有一个解决方案 Create to file Executable xxx.cmd, Open the file and write next text.

 @echo off

echo   - = = =   Schedule Run Jobs == = = = -


CD d: &&  CD \xampp\htdocs\folderlaravel && php artisan schedule:run

timeout 86400 

CD d: &&  CD \xampp\htdocs\folderlaravel && "Schedule.cmd"

pause

@cls

What you do is run and run itself in an infinite loop depending on what timeout you are given. In this case 86400 => 1 day.

您所做的是根据给定的超时时间在无限循环中运行和运行自己。在这种情况下,86400 => 1 天。

It is somewhat ambiguous but it works :)

这有点模棱两可,但它有效:)

I hope it works for you.

我希望这个对你有用。

回答by viswanath608

Windows does support Laravel Schedulerbut, you've to run the command on your own for multiple times. Since we can't use Windows Task Schedulerto run for every 1 min as we can do with linux crontab. If you're using windows for development environment and want to test if command is working on not you can try this

Windows 确实支持Laravel Scheduler,但是您必须自己多次运行该命令。因为我们不能像使用linux crontab那样使用Windows 任务计划程序每 1 分钟运行一次。如果您使用 windows 作为开发环境并想测试命令是否正在运行,您可以试试这个

If you run the

如果你运行

php artisan schedule:run

php工匠计划:运行

command for multiple times by giving a min gap for each trial it'll work.

通过为每次试验提供最小间隔来多次命令它会起作用。

enter image description here

在此处输入图片说明

If you want to run directly the command you can follow this.

如果你想直接运行命令,你可以按照这个。

"path\to\php.exe" "artisan" YourCommand > "NUL" 2>&1 &

"path\to\php.exe" "artisan" YourCommand > "NUL" 2>&1 &

You can find path of your php.exe using below step.

您可以使用以下步骤找到 php.exe 的路径。

Run "where php.exe" in command prompt

在命令提示符下运行“where php.exe”