<bdo id='sFqvp'></bdo><ul id='sFqvp'></ul>

  • <small id='sFqvp'></small><noframes id='sFqvp'>

    <tfoot id='sFqvp'></tfoot>
    <i id='sFqvp'><tr id='sFqvp'><dt id='sFqvp'><q id='sFqvp'><span id='sFqvp'><b id='sFqvp'><form id='sFqvp'><ins id='sFqvp'></ins><ul id='sFqvp'></ul><sub id='sFqvp'></sub></form><legend id='sFqvp'></legend><bdo id='sFqvp'><pre id='sFqvp'><center id='sFqvp'></center></pre></bdo></b><th id='sFqvp'></th></span></q></dt></tr></i><div id='sFqvp'><tfoot id='sFqvp'></tfoot><dl id='sFqvp'><fieldset id='sFqvp'></fieldset></dl></div>

  • <legend id='sFqvp'><style id='sFqvp'><dir id='sFqvp'><q id='sFqvp'></q></dir></style></legend>

      1. Yii2-使用调度程序在 Windows 上运行控制台应用程序

        时间:2023-10-11
        <i id='hJQ4A'><tr id='hJQ4A'><dt id='hJQ4A'><q id='hJQ4A'><span id='hJQ4A'><b id='hJQ4A'><form id='hJQ4A'><ins id='hJQ4A'></ins><ul id='hJQ4A'></ul><sub id='hJQ4A'></sub></form><legend id='hJQ4A'></legend><bdo id='hJQ4A'><pre id='hJQ4A'><center id='hJQ4A'></center></pre></bdo></b><th id='hJQ4A'></th></span></q></dt></tr></i><div id='hJQ4A'><tfoot id='hJQ4A'></tfoot><dl id='hJQ4A'><fieldset id='hJQ4A'></fieldset></dl></div>

            <bdo id='hJQ4A'></bdo><ul id='hJQ4A'></ul>
              <legend id='hJQ4A'><style id='hJQ4A'><dir id='hJQ4A'><q id='hJQ4A'></q></dir></style></legend>

              • <tfoot id='hJQ4A'></tfoot>

                    <tbody id='hJQ4A'></tbody>

                  <small id='hJQ4A'></small><noframes id='hJQ4A'>

                1. 本文介绍了Yii2-使用调度程序在 Windows 上运行控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发 yii2.我有一个创建函数,我想每 24 小时在后台运行一次.

                   公共函数 actionCreate(){$model = new MeterPing();//从 web-api 获取结果的 CURL 代码if ($model->load(Yii::$app->request->post()) && $model->save()) {返回 $this->redirect(['view', 'id' => $model->id]);} 别的 {返回 $this->render('create', ['模型' =>$模型,]);}}

                  同样在这个函数中,我将从 web-API 获取一些记录.但我主要关心的是每 24 小时后在后台运行上述函数.

                  更新 1

                  根据给出的建议,我在 consolecontroller

                  中创建了一个控制器

                  命名空间控制台控制器;使用 yiiconsoleController;类 TestController 扩展控制器 {公共函数 actionIndex() {echo "cron 服务正在运行";}}

                  现在,为了测试它,我遵循了这个

                  现在,按照建议我已经尝试了每一步..bat 文件包含

                  @Echo 关闭标题我的 Cron 工作:: 见顶部标题php E:xampphtdocsinventory-webyii test暂停

                  保存文件并将其添加到任务调度程序后.我试图从这个界面运行它.我确实看到一个 cmd 打开并显示消息.

                  我该怎么做?任何帮助将不胜感激.

                  解决方案

                  您可以使用 Task Scheduler for windows

                  • 创建一个 .bat 文件,如果你创建了一个控制器类 TestController,它应该包含运行你的 Yii 控制台应用程序 controller/action 的命令扩展 yiiconsolecontroller 并添加一个 create 操作,要从 Yii 控制台应用程序运行该操作,您将键入 ./yii test/create 使用 gitBash 或控制台在项目根目录上,因此我们将通过提供 project_root/yii 的完整路径(即 Yii 控制台引导程序)将此命令添加到 .bat 文件中文件.这是作为 Yii 控制器操作代码的一部分运行作业的主要内容.

                  打开记事本并将下面的代码复制到其中并另存为.bat,名称为my-cronjob.bat

                  @Echo 关闭标题我的 Cron 工作:: 见顶部标题php F:xampphtdocsmy-projectyii test/create暂停

                  注意:确保您在

                  • 为任务写一个名称

                  • 选择触发时间Daily

                  • 选择执行任务的时间

                  • 选择Action启动程序

                  • 现在选择 my-cronjob.bat 文件,然后按 Next 和 Finish

                  • 现在转到任务管理器并选择 Task Scheduler Library 并右键单击您刚刚创建的 cronjob 并打开属性.

                  • 选择以最高权限运行

                  注意:您可以选择在用户登录或未登录时运行 cronjob,默认情况下它仅在用户登录时运行,您可以更改该选项.

                  就是这样,您现在可以通过右键单击并选择运行来手动运行任务,它将运行 my-cronjob.bat 中指定的所需 controller/action代码>或等待它在指定时间触发.

                  用于后台运行

                  以上设置将打开命令提示符并运行任务,如果您希望调度程序最小化运行任务,您应该看到这个link 并相应地更新任务设置.此外,您也可以将最后一行 pause 更改为 Exit.

                  我在此处发布答案之前已经对其进行了测试,因为我家里有 Windows10 操作系统,因此无法在没有办公室测试的情况下发布答案.

                  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,
                          ]);
                      }
                  }
                  

                  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.

                  Update 1

                  As per suggestion was given I have created a controller in consolecontroller

                  namespace consolecontrollers;
                  
                  use yiiconsoleController;
                  
                  class TestController extends Controller {
                  
                  public function actionIndex() {
                      echo "cron service runnning";
                  }}
                  

                  Now, for testing it I followed this link and created a run service using cmd with following parameters

                  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:xampphtdocsinventory-webyii test
                  pause
                  

                  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.

                  解决方案

                  You can use Task Scheduler for windows

                  • Create a .bat file which should contain the command for running your Yii console app controller/action if you create a controller class TestController that extends yiiconsolecontroller and add a create 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 the project_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.

                  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:xampphtdocsmy-projectyii test/create
                  pause
                  

                  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

                  Steps to create a Task Scheduler

                  • Type in Task Scheduler in the start menu and open it.

                  • Create a basic Task

                  • Write a name for the task

                  • Select Trigger time Daily

                  • Select time to execute the task

                  • Select Action Start a programme

                  • Now select the my-cronjob.bat file and press Next and Finish

                  • Now go to task manager and select Task Scheduler Library and right click the cronjob you just created and open properties.

                  • Select Run with highest privileges

                  NOTE: you can select to run the cronjob if the user is logged in or not, by default it only runs if the user is logged in you can change that option.

                  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.

                  For running in the background

                  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.

                  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-使用调度程序在 Windows 上运行控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tfoot id='dimG1'></tfoot><legend id='dimG1'><style id='dimG1'><dir id='dimG1'><q id='dimG1'></q></dir></style></legend>

                      1. <small id='dimG1'></small><noframes id='dimG1'>

                          <tbody id='dimG1'></tbody>
                        • <bdo id='dimG1'></bdo><ul id='dimG1'></ul>

                        • <i id='dimG1'><tr id='dimG1'><dt id='dimG1'><q id='dimG1'><span id='dimG1'><b id='dimG1'><form id='dimG1'><ins id='dimG1'></ins><ul id='dimG1'></ul><sub id='dimG1'></sub></form><legend id='dimG1'></legend><bdo id='dimG1'><pre id='dimG1'><center id='dimG1'></center></pre></bdo></b><th id='dimG1'></th></span></q></dt></tr></i><div id='dimG1'><tfoot id='dimG1'></tfoot><dl id='dimG1'><fieldset id='dimG1'></fieldset></dl></div>