Laravel 5.1 Task Scheduling on Windows

浪尽此生 提交于 2019-12-21 03:16:16

问题


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?

Windows batch file:

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');
    }

回答1:


Take a look at the task scheduler doc.

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.

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

artisan schedule:run does 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.




回答2:


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

To do so :

  • Press Win + R and run taskschd.msc

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

  • Click Next and select Start a Program option, 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 Trigger tab, you can change between Daily or At Logon (as I do).

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

    Larevel needs the cronjob to run every minute, otherwise it won't work as expected.

    Also check "Indefinitely" to run it forlifetime.

Hope it helps.

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




回答3:


I have a single solution Create to file Executable xxx.cmd, Open the file and write the 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.

It is somewhat ambiguous but it works :)

I hope it works for you.




回答4:


Windows does support Laravel Scheduler but, you've to run the command on your own for multiple times. Since we can't use Windows Task Scheduler to 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

If you run the

php artisan schedule:run

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

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

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

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

Run "where php.exe" in command prompt



来源:https://stackoverflow.com/questions/32950083/laravel-5-1-task-scheduling-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!