沃梦达 / 编程问答 / php问题 / 正文

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

Yii2- Run Console app on windows using Scheduler(Yii2-使用调度程序在 Windows 上运行控制台应用程序)

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

本文标题为:Yii2-使用调度程序在 Windows 上运行控制台应用程序

基础教程推荐