How to run task scheduler in windows 10 with Laravel - laravel

I just want to ask how to use it's task scheduling feature on windows machine (my local machine).
I've read it's documentation and I've notice that it's using a Cron.
Any help is truly appreciated.

To run Laravel Scheduler in Windows 10 you need:
Create batch file, like this one and save it:
cd c:\laravel-project\
c:\php5\php.exe artisan schedule:run 1>> NUL 2>&1
Go to Windows 10 Task Scheduler (fast way is press Win+R and enter taskschd.msc).
Click Create basic task, choose When I logon trigger and then choose Start a program -> your .bat file.
Check Open properties dialog option and click Finish.
In task properties click Triggers, then click New and add new trigger Repeat task every - 1 minute.
Now this task will run Laravel scheduler every one minute.

I still did not run the schedule, the solution was just to add / d to the path in
cd c:\laravel-project\
in
cd /d c:\laravel-project\

Related

Using a batch file to start pm2 when windows starts?

I can't get pm2 to start my apps on Windows start. I'm running Windows Server 2012 R2 Standard and pm2 2.4.2.
I have a pm2 process file in JSON format which I use to start all my apps.
c:\pm2\process.json
{
"apps": [
{
"name" : "my-app",
"script" : "c:\\node\\myapp\index.js"
}
]
}
I have a batch file which uses the JSON file:
c:\pm2\pm2-startup.bat
#echo off
set HOMEDRIVE=C:
set PM2_HOME=C:\etc\.pm2
setx /M PM2_HOME C:\etc\.pm2
cd C:\pm2 & pm2 start process.json
I have a Windows task scheduled to run the batch file:
Trigger: At startup
Run under: An administrator account
Run whether user is logged in or not: Yes
Run with highest privileges: Yes
Action: Start a program
Script: C:\pm2\pm2-startup.bat
Start in: C:\pm2
If I run the batch file manually (double clicking it), it works. If I run the scheduled task manually (right-click, run), it works.
When I restart the server and check the scheduled task, it has run, no errors, however the apps are not running. Doing pm2 list shows no apps in the table.
I don't want to use pm2-windows-service because I don't want to run pm2 as a service (tried it and it was flaky).
I don't want to use pm2-windows-startup either as it doesn't seem to work with a pm2 process file, it just tries to remember what was running before.
I do want to use a plain batch file on startup.
What am I doing wrong with the batch file..? Why does the scheduled task run ok, but the pm2 list is empty..?
I know this is an old question, but I'll leave an answer for future searches.
You can put a shortcut to the .bat file in the Windows Startup folder.
Assuming the PM2 cmd is in path, your bat file could look like this.
#echo off
SET PM2_HOME=C:\Users\pstart\.pm2
pm2 start C:\path\to\run.js
That'll start PM2 and run the program. Now you just take the bat file and save it somewhere and create a shortcut in the Windows Startup folder by clicking the Start button -> Run -> shell:startup and pasting it in there.

Command Prompt doesn't close after scheduled task runs

I have a job scheduled to run at 9:30 every day using Windows task scheduler. The problem is after it runs, the command prompt stays open. Does anyone know how to get it to close?
The full text in the "Add arguments (optional):" field is:
C:\WinPython64bit\notebooks\TreasuryTest.py exit 0
I have searched all over, but most fixes are for use directly in the command prompt, and it seems to function differently from the task scheduler.

running shell script with windows task scheduler

I currenty have a simple shell script that I created for a linux machine to be run using cron, but now I want to be able to run the file using windows task scheduler. I have tried to get it to work using cron for cygwin, but even after running cron-config successfully and ensuring that the shell script can be executed successfully, for some reason the cron task simply wasn't executing. So I decided to give in and use the windows task scheduler. In order to do this, I looked at the following posts about the issue:
Cgywin .sh file run as Windows Task Scheduler
http://www.davidjnice.com/cygwin_scheduled_tasks.html
in my case, the entry in the "actions" tab of the new task looks like this:
program/script: c:\cygwin64\bin\bash.exe
arguments: -l -c "/cygdrive/c/users/paul/bitcoinbot/download_all_data.sh >> cygdrive/c/users/paul/bitcoinbot/logfile.log 2>&1"
start in: c:\cygwin64\bin
Notice that I redirected the output of the shell script to a log file, so that I should be able to see there whether the program run. Other than that, I simply edited the "trigger" tab to run the task daily, and set the time to a couple of minutes in the fture to see whether it ran successfully.
Alas, when I look at the detailed event history for the task, nothing changes when the trigger time passes. And when I manually "run" the task, the event history seems to add a few different events, but the task is completed within seconds, whereas this task should take over an hour (and it does when the shell script is executed directly from the terminal). And when I look for the log file that should have been created, there is nothing.
Does anyone have any idea what might be the issue here? How can I get my task to run properly at the trigger time, and how can I make sure it does so?
Best,
Paul
EDIT:
here are the pictures showing event history, as per Ken White's request.
Please ignore the fact that it says there are 24 events. These are from multiple separate runs of the task. The events shown here are a complete list of the events triggered by a single run.
EDIT 2:
Regarding my attempts to get cron to work, I have run into the following problem when I try to start the cron service using cygrunsrv. First of all, I tried to start cron by typing
cygrunsrv -I cron -p /usr/sbin/cron.exe -a -D
Now when I type
$cygrunsrv -Q cron
Service: cron
Current State: stopped
Command: /usr/bin/cron.exe
Now, I tried to start the cron service by typing
cygrunsrv -S cron
Cygrunsrv: Error starting a service: QueryServiceStatus: Win32 error 1062:
The service has not been started.
Does anyone hae any idea what this error means? I tried googling it, but couldn't find any answers.

Laravel 5.1 Task Scheduling on Windows

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

How does Windows Task Scheduler in Win7 recognize a failed task?

I am working with Windows 7 and I have an application that returns zero (0x0) when successful and one (0x1) on error situations.
I have scheduled this app using Windows Task Scheduler. I have checked the option boxes "If the task fails, restart every" and "Attempt to restart up to:".
I thought that a non-zero return code from the app would be enough to trigger the task to be restarted after the given interval. But nothing happens.
Any ideas what could be the issue? I tried to google it but did not found anything relevant.
Create a new task and set the custom event query like this:
<QueryList>
<Query Id="0" Path="Microsoft-Windows-TaskScheduler/Operational">
<Select Path="Microsoft-Windows-TaskScheduler/Operational">*[System[EventID=201]] and *[EventData[Data[#Name='ResultCode']!='0']] and *[EventData[Data[#Name='TaskName']='\YOUR TASK NAME HERE']]</Select>
</Query>
</QueryList>
Set the trigger advanced settings to Delay the task for a period of time like 15 minutes.
Configure the action of the new task to start a program:
Program/script:
schtasks
Add arguments:
/Run /TN "\YOUR TASK NAME HERE"
This will schedule the original task to run again 15 minutes after a non-zero result code is logged in the event.
I've experienced the same problem on a Windows 2008 server Windows Task Scheduler.
The action return a non zero code but the scheduler consider the task completed:
Task Scheduler successfully completed task "\SET Tasks\Scheduled task [Backup SET Server]" , instance "{...}" , action "C:\Windows\SYSTEM32\cmd.exe" with return code 1.
I've found on the web only one answer:
The Windows Task Scheduler does not
examine the exit code or any other
values when your task completes. You
must handle any error processing
within your own script or program.
...in this document: www.onlinetoolworks.com/docs/winTaskSched.doc
So I think now that the only way to workaround this problem may be to use task triggering on event. I'm investigating.
Regards,
Olivier.
You can,
activate history for Schedule (if not already)
on a History "Action completed" right click "Attached Task to This Event..."
Set a custom filter like this:
*[System[(EventID=201)]] and *[EventData[Data[#Name='ResultCode']='1']]
Enjoy
Tilo
used on Win 2008 R2 (Exchange as email server)
Nobody has answered the title question though. It seems as though the task scheduler has no way to detect a failed task? Surely it must have something because it has an option to restart failed tasks!
The option "If the task fails, restart" is misleading, a failed task is when Task Scheduler is not able to run one of the action correctly, not when the script itself runs and return an error code.
A very clever workaround suggested here can be used instead of additional task querying for events:
The issue is the actions are only considered to fail if they cannot be started. This is pretty dumb, but the scheduler doesn't care about the results of the actions.
One workaround is to add an action at the end to run something like "ok.exe" and then have your other actions either create "ok.exe" (good result) or delete it. (bad result)
This way when the Task Scheduler goes to run the last action it will fail to start it (if you had removed it, because your previous action failed). This will cause the Task Scheduler to go ahead and Queue up your Task for a restart based on the restart settings on the Scheduled Task.
PS: To create a dummy ok.exe, I usually just copy c:\windows\system32\clip.exe c:\mytask\ok.exe
-- Tolga
So what is suggested is to create a final action for the task that will call a dummy .exe file. If the .exe file is not found, this will correctly cause the failure for the task and trigger the restart option. As .exe file he uses clip.exe from Windows system folder, this is nice I think because is very small and won't do any action if called on it's own.
Caution
I've tried with empty .cmd and .bat files but this will not trigger correctly the fail trigger. It must be a .exe file apparently.
Of course a mechanism to rename/move the ok.exe file in case your target script/job fails is needed.
I've pointed the Task Scheduler to use the wrapper script below with success. Edit the path to ok.exe as needed, I've copied it in the same directory as this wrapper script:
#echo off
:: This will reset the .exe name to make sure the .exe is available if error is
:: not triggered. See `:trigger_error`.
if exist ok.exe.nope (
rename ok.exe.nope ok.exe
)
:: This script is used as batch runner by Task Scheduler.
:: MAKE SURE THE SCRIPT WILL RETURN AN ERROR CODE !=0
echo Here is my script!
:: If script above exited with an error, rename the `ok.exe` file. This will
:: cause a failure for Task Scheduler following action attempting to run
:: `ok.exe`, thus triggering the restart conditions.
:: See https://social.technet.microsoft.com/Forums/Lync/en-US/4545361c-cc1f-4505-a0a1-c2dcc094109a/restarting-scheduled-task-that-has-failed#e4e3ff74-2d42-4d58-a930-a7838a0762ff
:trigger_error
if %errorLevel% NEQ 0 (
rename ok.exe ok.exe.nope
)

Resources