2

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');
    }
suncoastkid
  • 2,193
  • 7
  • 26
  • 45

4 Answers4

4

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.

Community
  • 1
  • 1
bernie
  • 9,820
  • 5
  • 62
  • 92
  • I have run the following command But couldn't have the idea where the batch file actually? `C:\>cd xampp/htdocs/blog` `C:\xampp\htdocs\blog>cd c:/xampp/php/php.exe artisan emails:send 1>> NUL 2>&1` – Hola Aug 16 '16 at 06:18
4

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.

Chait
  • 1,052
  • 2
  • 18
  • 30
Dario Corno
  • 1,129
  • 11
  • 19
  • Where does the batch file actually stored? After running those command? – Hola Aug 16 '16 at 06:15
  • It doesn't really matter if you put the CD commands to move to the base laravel installation. eg: CD \your\laravel\base\folder – Dario Corno Aug 16 '16 at 14:11
  • No actually what i wanted to say is while i have to upload the batch file in task scheduler creation i don't get which file should I upload? – Hola Aug 16 '16 at 15:02
  • I don't really know, it depends on your server setting where an uploaded file ends :D – Dario Corno Aug 17 '16 at 13:55
1

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.

0

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.

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 &

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

Run "where php.exe" in command prompt

viswanath608
  • 155
  • 2
  • 7
  • As you can see in my answer, actually THERE IS a way to make the windows scheduler run a command every minute. Laravel won't work with any other cron timings. – Dario Corno Aug 16 '16 at 14:14