Laravel 5.1 Task Scheduling on Windows - laravel

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

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.

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.

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.

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

Related

Laravel 7: Cron jobs for task scheduler in Cpanel not working

I'm using Namecheap Hosting. And my files are in a subdomain. I've added a task scheduler in Kernel.php file and add cron jobs in hosting. But it's not working. When I manually run schedule command it works perfectly. Can anyone please see the below code and details and tell me what am I missing?
Kerner.php file:
protected function schedule(Schedule $schedule)
{
$schedule->command('add:earnLeave')
->everyMinute()
->timezone('Asia/Dhaka');
}
Cron Command:
/usr/local/bin/php /https://subdomain.maindomain.com/php artisan schedule:run >> /dev/null 2>&1
and in my hosting the minimum run time is every five minutes */5 * * * *
and my task scheduler will run once on the last day of the month. For testing purposes, I set it to every minute. Should I have to keep this run time equal to cron jobs?
What should I do now?
Domain name no needed.
/usr/local/bin/php /home/username/public_html/laravelrootfolder/artisan schedule:run >> /dev/null 2>&1
I have solved this problem myself.
I use /usr/bin/php instead of usr/local/bin/php
And also proc_open was disabled. After enabling it, now it's working fine.

How to run schedule tasks every minute in Laravel?

How i can run my Schedule task every minute? When i run command php artisan schedule:run, console get "No scheduled commands are ready to run.". How i should set my Tasks properly? For example if I use solo command php artisan command:send_info_email_before_event it works, but i want do it with schedule:run when i develop app.
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('command:send_info_email_before_event')
->everyMinute();
}
You should be setting this up via cron job to run every minute. Task scheduling is not intended to be run manually.
In the terminal run:
crontab -e
Then add:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Once saved, this will then run the application scheduler every minute (without you needing to run php artisan schedule:run.
See the docs for more information.
In windows you can schedule a task.
Open the Windows start menu and type in "Scheduled Tasks". This will
open the Task Scheduler. After that click Create Task (I would not
suggest using basic task, as it is too basic for cron jobs).
This will open a window that allows you to create a new "Cron Job" in Windows 10.
Name: This is an easy way for you to remember what that task does. You can enter anything.
Description: Just a more in-depth description for you to remember what it is.
User Account: I would suggest using a full privileged account so that your cron jobs can create and modify files if it needs to. But I would also suggest using an account dedicated to Cron jobs so that you can revoke permissions any time you need to!
Due to the nature of a website, I would definitely check “Run when user is logged in or not” and leave do not store password (This can throw an XML invalid error if you leave this unchecked and a password is required)
And I would also check "Run with Highest Privileges"
After that, Click "Trigger" and "Add new"
To run a "Cron Job" task that runs more than once a day, Click Daily task. Recure daily, and repeat the task every minute. Be sure to click enabled. After that click Okay and click the tab "Actions" and New…
Set program/Script to the /path/to/php.exe and arguments to /path/to/artisan schedule run. Be sure to use "" if you have spaces in the path. I would suggest using them either way as a rule of thumb.
Remember to not use production email credentials, you can use something like https://mailtrap.io to "trap" all sent emails.

Laravel Artisan: How does `schedule:run` work?

I have a dummy Command job set up, whose handle() function is as follows:
public function handle()
{
$this->line('==================');
$this->line('Running my job at ' . Carbon::now());
$this->line('Ending my job at ' . Carbon::now());
}
As you see, it doesn't actually do anything but return a few lines of info to the standard output.
Now, in my App\Console\Kernel class, I have set up the following schedule:
protected function schedule(Schedule $schedule)
{
$schedule
-> command('cbh:dummyCommand')
-> everyMinute()
-> appendOutputTo (storage_path().'/logs/laravel_output.log');
}
Now, from the command-line I run php artisan schedule:run. The output in my laravel_output.log file reads
==================
Running my job at 2018-02-08 11:01:33
Ending my job at 2018-02-08 11:01:33
So far so good. It seems that my schedule is running. However, if I run the command again within the same minute, my logfile now reads:
==================
Running my job at 2018-02-08 11:01:33
Ending my job at 2018-02-08 11:01:33
==================
Running my job at 2018-02-08 11:01:51
Ending my job at 2018-02-08 11:01:51
In other words, the schedule appears to be running more frequently than every minute, which appears to me to break the rules I defined in my schedule.
What's more confusing is that I can change the schedule to run every 5 minutes instead of every minute:
protected function schedule(Schedule $schedule)
{
$schedule
-> command('cbh:dummyCommand')
-> everyFiveMinutes()
-> appendOutputTo (storage_path().'/logs/laravel_output.log');
}
then run php artisan schedule:run, then I get the following output
No scheduled commands are ready to run.
I can wait as long as you like (i.e. more than 5 minutes) and still I get no output to my log file.
I observe exactly the same behaviour when I schedule my command with Windows Task Scheduler (yes, my development environment is a Windows 7 box, and yes, this is the Windows equivalent of a cron-job).
The Question
So what's going on? How does the artisan schedule:run command figure out which commands are "waiting" on the schedule to be executed? I had imagined that there would be some kind of log-file to record the fact that "Command X is on a 1-hour schedule and last ran at 09:00, so don't execute it again before 10:00", but I have been able to find no trace of such a log.
Can someone give me a clue?
Thanks!!
Not cool to answer your own question, I know. Anyhow, let's imagine this is my schedule:
protected function schedule(Schedule $schedule)
{
$schedule
-> command('cbh:dummyCommand')
-> everyFiveMinutes()
-> appendOutputTo ('/my/logs/laravel_output.log');
}
What I've discovered is that this code doesn't set your job to run every 5 minutes. Nor does it prevent the command running again if it was run less than 5-minutes ago.
A better way to think about it is that this code sets the named command "to be runnable every time the minute-figure of the current time is 0 or 5". In other words, if I run the command-line argument: php artisan schedule:run at 11:04, then the response is:
# No scheduled commands are ready to run.
But if I run the same command at 11:00 or 11:05, then we get:
# Running scheduled command: php artisan cbh:dummyCommand >> /my/logs/laravel_output.log 2>&1
And I end up with output in my log-file.
I discovered the above when my everyFiveMinutes() schedule was creating a log in my file every 10 minutes based on the fact that my task-scheduler was running every 2 minutes.
I'm answering this just to let other people know it (since I was having the same confusion).
Laravel scheduler does exactly the same job than Linux cron, by checking if a task cronned time (in minutes) is exactly the same of current time.
When you set in crontab * * * * * ... php artisan schedule:run >> ... you are running schedule:run every minute at 0 secs, like '1:00:00', '1:01:00', '1:02:00', etc.
So, if you set your command to run (let's say) on 'mondays at 1:00' in your Laravel scheduler, and you are on a monday at 1:00, it will be executed, regardless the current seconds. And the last part (seconds) is important to understand how it works.
For example, you are on monday at 1:00:05 (5 seconds after 1:00), so cron already launched schedule:run and your task is being executed. Then you open your terminal, go to your project's root directory and launch, manually, php artisan schedule:run. At that time, it may be 1:00:30 (30 seconds after 1:00). Well, now your task will be executed again because 1:00:30 is still part of 1:00. So you can execute N times schedule:run at 1:00 and it will execute N times your task scheduled to run at 1:00.
And that's the magic of no needing a table or a file to control process launching time. Minutes is the minimum unit in cron, so unless you are doing the things wrong (like duplicating schedule:run line, a hack to run a command more often than a minute, etc.) your Laravel tasks will be executing once at the desired time.
Just a note: Check that your timezone is correct in config/app.php. I got crazy to understand why things like everyMinute(), everyFiveMinutes() were working, and dailyAt('1:10') were not. Of course, with Laravel in UTC and me being in GMT-3 (server clock), I had a great difference in hours.

Laravel Task Scheduling set to run every minute but it run only once

I am using Laravel Task Scheduling. I defined a custom command and set it to run every minute like:
$schedule->command('dailyk')->everyMinute();
Then I used the following command to run the task:
php /var/www/stockhit/artisan schedule:run 1>> /dev/null 2>&1
I used log to check that my custom command continued to run. However, it is not run every minute. Instead, it just ran once.
How can I make it run every minute, instead of just one time?
See Task Scheduling:
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.
Laravel's task scheduler does not stay in memory, it needs to be run every minute. It will then check which tasks need to be run in that minute and run them.
When you run the task scheduler using PHP it just runs once, it needs cron to run it every minute.
you need to add a cron job. On ubuntu use the command
crontab -e
to open your cron job file, then add
* * * * * php /var/www/stockhit/artisan schedule:run 1>> /dev/null 2>&1

Laravel Schedule command does not work

I used laravel 5 in my project. I wanted to create a scheduler for inserting user. In kernel.php, I put my codes and set the scheduler.
I created a command class named "InsertUser" and put it in kernel.php $commands variable.
In command line, I ran "php artisan schedule:run". But I found "No scheduled commands are ready to run.". If I used call function instead of command function (in lernel.php), it was working fine. Please help me.
The Laravel Scheduler needs a cron job that runs the php artisan schedule:run command periodically, which in turn evaluates any scheduled commands and runs them accordingly.
From your screenshot I see you're running Windows, which means you can't use the job code snippet from the Starting The Scheduler section in the documentation because there is no cron on Windows. Windows is not officially supported for the task scheduler because of that and no instructions for it can be found in the documentation.
You could however get around the problem by creating a batch file, say scheduler.bat, that has the following contents:
cd c:\lamp\www\larasoft
php artisan schedule:run 1>> NUL 2>&1
Then you can add a Windows Scheduler Task to run that file every minute.
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 &
In your case
Run "where php.exe" in command prompt
Copy The php location
"paste\your\php\location" "artisan" InsertUser > "NUL" 2>&1 &

Resources