Home Backend Development PHP Tutorial Detailed explanation of the steps to create scheduled tasks through console commands in the Yii framework

Detailed explanation of the steps to create scheduled tasks through console commands in the Yii framework

May 21, 2018 am 09:41 AM
create Order console

This time I will bring you a detailed explanation of the steps to create a scheduled task through the console command in the yii framework. What are the precautions for creating a scheduled task through the console command in the yii framework. The following is a practical case, let’s take a look. one time.

Assume that the Yii project path is /home/apps/

1, create the file /home/apps/protected/commands/crons.php

1

2

3

4

5

6

7

8

<?php

$yii = &#39;/home/apps/framework/yii.php&#39;;

require_once

($yii);

$configFile =

dirname

(FILE).&#39;/../config/console.php&#39;;

Yii::createConsoleApplication($configFile)->run();

Copy after login

2, create the required

Configuration file /home/apps/protected/config/console.php, configure required components, database connections, logs and other information, the format is similar to the main configuration file main.php.

1

2

<?php  return array(     &#39;basePath&#39;=>dirname(FILE).DIRECTORY_SEPARATOR.&#39;..&#39;,    &#39;name&#39;=>&#39;Emergency&#39;,    &#39;import&#39;=>array(              &#39;application.models.*&#39;,            &#39;application.components.*&#39;,             &#39;application.extensions.*&#39;,    ),    &#39;components&#39;=>array(        &#39;log&#39;=>array(            &#39;class&#39;=>&#39;CLogRouter&#39;,            &#39;routes&#39;=>array(                array(                    &#39;class&#39;=>&#39;CFileLogRoute&#39;,                    &#39;levels&#39;=>&#39;info, warning, error&#39;,                ),            ),        ),         &#39;db&#39;=>array(            &#39;class&#39;=>&#39;application.extensions.PHPPDO.CPdoDbConnection&#39;,            &#39;pdoClass&#39; => &#39;PHPPDO&#39;,            &#39;connectionString&#39; => &#39;mysql:host=xxxx;dbname=xxx&#39;,            &#39;emulatePrepare&#39; => true,            &#39;username&#39; => &#39;xxx&#39;,            &#39;password&#39; => &#39;xxx&#39;,            &#39;charset&#39; => &#39;utf8&#39;,            &#39;tablePrefix&#39; => &#39;tbl_&#39;,        ),    ),

    &#39;params&#39; => require(&#39;params.php&#39;),);

Copy after login

3. Create a new TestCommand class under /home/apps/protected/commands/ and inherit CConsoleCommand. In TestCommand, you can use the project's configuration information and Yii's various methods.


1

2

3

4

5

6

7

8

9

10

<?php 

class TestCommand 

extends

 CConsoleCommand 

    public function run()

    

        ...

    

}

Copy after login

4, create a scheduled task

$ crontab -e
The content is:

1 * * * * /home/php/bin/php -f /home /apps/protected/commands/crons.php Test & is to execute the contents of the TestCommand class in the first minute of every hour. Similarly, you can create other classes under /home/apps/protected/commands/ and use the command Execute.

For the usage of crontab, please refer to:

crontab command basics and examples Some examples of crontab commands Detailed explanation of linux installation crontab crontab study notes Learn the crontab command to set up scheduled tasks in Linux Crontab command format and examples Linux scheduled task setting crontab learning Good crontab tutorial

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the use case of volist tag in thinkphp

Detailed explanation of the steps for printing binary trees in Z-shaped order in PHP

thinkWhat are the precautions for using the volist tag in ajax in thinkphp

The above is the detailed content of Detailed explanation of the steps to create scheduled tasks through console commands in the Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run SUDO commands in Windows 11/10 How to run SUDO commands in Windows 11/10 Mar 09, 2024 am 09:50 AM

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

Xbox System Error E200 [Fixed] Xbox System Error E200 [Fixed] Feb 19, 2024 pm 02:39 PM

This article will explain how to resolve system error E200 on your Xbox console. Typically, this error occurs when your Xbox console gets interrupted while trying to install the latest console operating system update. This error may also occur if the system update is interrupted due to a power outage or network problem. Fix Xbox System Error E200 Use the following fix to fix System Error E200 on your Xbox console: Turn your Xbox console off and on again Perform a system update factory reset your console Let’s get started. 1] Turning your Xbox console off and on again Resetting the power cycle of your Xbox console can effectively eliminate potential temporary glitches and resolve some issues. Follow these steps to turn off and reopen Xbox Control

How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 Feb 29, 2024 pm 04:34 PM

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

Super practical! Sar commands that will make you a Linux master Super practical! Sar commands that will make you a Linux master Mar 01, 2024 am 08:01 AM

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Feb 29, 2024 pm 05:52 PM

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

How to create a folder on Realme Phone? How to create a folder on Realme Phone? Mar 23, 2024 pm 02:30 PM

Title: Realme Phone Beginner’s Guide: How to Create Folders on Realme Phone? In today's society, mobile phones have become an indispensable tool in people's lives. As a popular smartphone brand, Realme Phone is loved by users for its simple and practical operating system. In the process of using Realme phones, many people may encounter situations where they need to organize files and applications on their phones, and creating folders is an effective way. This article will introduce how to create folders on Realme phones to help users better manage their phone content. No.

How to create pixel art in GIMP How to create pixel art in GIMP Feb 19, 2024 pm 03:24 PM

This article will interest you if you are interested in using GIMP for pixel art creation on Windows. GIMP is a well-known graphics editing software that is not only free and open source, but also helps users create beautiful images and designs easily. In addition to being suitable for beginners and professional designers alike, GIMP can also be used to create pixel art, a form of digital art that utilizes pixels as the only building blocks for drawing and creating. How to Create Pixel Art in GIMP Here are the main steps to create pixel pictures using GIMP on a Windows PC: Download and install GIMP, then launch the application. Create a new image. Resize width and height. Select the pencil tool. Set the brush type to pixels. set up

How to create a family with Gree+ How to create a family with Gree+ Mar 01, 2024 pm 12:40 PM

Many friends expressed that they want to know how to create a family in Gree+ software. Here is the operation method for you. Friends who want to know more, come and take a look with me. First, open the Gree+ software on your mobile phone and log in. Then, in the options bar at the bottom of the page, click the "My" option on the far right to enter the personal account page. 2. After coming to my page, there is a "Create Family" option under "Family". After finding it, click on it to enter. 3. Next jump to the page to create a family, enter the family name to be set in the input box according to the prompts, and click the "Save" button in the upper right corner after entering it. 4. Finally, a "save successfully" prompt will pop up at the bottom of the page, indicating that the family has been successfully created.

See all articles