Yii2-使用Scheduler在Windows上运行控制台应用程序 [英] Yii2- Run Console app on windows using Scheduler
问题描述
我正在研究yii2
.我有一个create函数,我想每24小时在后台运行一次.
I am working on yii2
. I have a create function which I want to run in the background after every 24 hours.
public function actionCreate()
{
$model = new MeterPing();
// CURL code to get results from web-api
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
在此功能中,我还将从web-API
获取一些记录.但是我主要关心的是每隔24小时在后台运行一次上述功能.
Also in this function, I will be fetching some records from a web-API
. But my main concern is to run the above function in the background after every 24 hours.
更新1
根据建议,我在console\controller
namespace console\controllers;
use yii\console\Controller;
class TestController extends Controller {
public function actionIndex() {
echo "cron service runnning";
}}
现在,为了进行测试,我遵循了以下 link 并使用cmd
和以下参数
Now, for testing it I followed this link and created a run service using cmd
with following parameters
现在,按照建议进行操作,我已经尝试了每个步骤. .bat
文件包含
Now, by following as per suggested I have tried each step. The .bat
file contains
@Echo off
title My Cron Job
:: See title at the top
php E:\xampp\htdocs\inventory-web\yii test
pause
保存文件并将其添加到任务计划程序之后.我试图从此界面运行它.我确实看到一个cmd打开并显示了消息.
After saving the file and adding it to the task scheduler. I tried to run it from this interface. And I did see a cmd opening and showing the message.
我该怎么办?任何帮助将不胜感激.
How can I do it? Any help would be highly appreciated.
推荐答案
您可以为Windows使用Task Scheduler
You can use Task Scheduler for windows
- 创建一个
.bat
文件,如果您创建一个扩展yii\console\controller
的控制器类TestController
并添加一个create
操作,则该文件应包含用于运行Yii控制台应用程序controller/action
的命令,以便从以下位置运行该操作您将使用gitBash或console在项目根目录上键入./yii test/create
的Yii控制台应用程序,因此我们将通过提供project_root/yii
的完整路径(即Yii控制台引导文件)来将此命令添加到.bat
文件中.这是将作业作为Yii控制器动作代码的一部分进行操作的主要内容.
- Create a
.bat
file which should contain the command for running your Yii console appcontroller/action
if you create a controller classTestController
that extendsyii\console\controller
and add acreate
action, to run that action from Yii console application you would type./yii test/create
on the project root using gitBash or console, so we will add this command to.bat
file by providing the full path to theproject_root/yii
which is the Yii console bootstrap file. This is the main thing to run the job as part of Yii controller action code.
打开记事本并在下面的代码中复制一个另存为.bat
且名称为my-cronjob.bat
open notepad and copy below code in it an save as .bat
with name my-cronjob.bat
@Echo off
title My Cron Job
:: See title at the top
php F:\xampp\htdocs\my-project\yii test/create
pause
注意:确保 Windows Path Variable
,或者您应该在上述.bat
文件中提供php.exe
的完整路径
NOTE: make sure you have php
in Windows Path Variable
or you should provide the complete path to php.exe
in the above .bat
file
-
在开始菜单中键入Task Scheduler,然后将其打开.
Type in Task Scheduler in the start menu and open it.
创建基本任务
- 为任务写一个名字
- 选择触发时间
Daily
- 选择执行任务的时间
- 选择
Action
启动程序
- 现在选择
my-cronjob.bat
文件,然后按Next和Finish
- Now select the
my-cronjob.bat
file and press Next and Finish
- 现在转到任务管理器并选择
Task Scheduler Library
,然后右键单击刚创建的cronjob并打开属性.
- Now go to task manager and select
Task Scheduler Library
and right click the cronjob you just created and open properties.
- 选择
Run with highest privileges
- Select
Run with highest privileges
注意:您可以选择运行cronjob(无论用户是否登录),默认情况下,仅在用户登录后才能运行cronjob,您可以更改该选项.
现在,您可以通过右键单击并选择运行来手动运行任务,它将运行my-cronjob.bat
中指定的所需controller/action
或等待它在指定时间触发.
That is it now you can either manually run the task by right-clicking and selecting run and it will run the desired controller/action
specified in the my-cronjob.bat
or wait for it to trigger on the specified time.
以上设置将打开命令提示符并运行任务,如果希望调度程序最小化运行任务,则应看到此
The above settings will open the command prompt and run the task if you want the scheduler to run the task minimized you should see this link
and update the task settings accordingly. Also, you can change the last line pause
to Exit
too.
我已经在此处测试答案之前对其进行了测试,因为我家里有Windows10
操作系统,因此如果不经过办公室的测试就无法发布答案.
I have tested it before posting the answer here as I have Windows10
OS at my home, so couldn't post the answer without testing from office.
这篇关于Yii2-使用Scheduler在Windows上运行控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!