Cron scheduled task applied to Thinkphp

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to apply Cron scheduled tasks to Thinkphp.
First, let's take a look at the Crontab command, and finally introduce how to apply it to Thinkphp. If you have already understood the Crontab command, you can pull it to the end to see how to apply it to Thinkphp.

Install crontab:

Yum install crontabs


Some cron commands:


/Sbin/service crond start // start the service

/Sbin/service crond stop // close the service

/Sbin/service crond restart // restart the service

/Sbin/service crond reload // reload the configuration

View crontab service status: service crond status

Start the crontab service manually: service crond start

Check whether the crontab service is set to start at startup. Run the following command: ntsysv.

Add boot auto start: chkconfig-level 35 crond on


Crontab command Introduction


Function Description: sets a timer.


Syntax: crontab [-u <用户名称> ] [Configuration file] or crontab [-u <用户名称> ] [-Elr]



Note: cron is a resident service that provides the timer function to allow users to execute preset commands or programs at specific times. You can use the timer function as long as you edit the timer configuration file. The configuration file format is as follows:

Minute Hour Day Month DayOFWeek Command



Parameters:

-E: edit the timer settings of the user.

-L list the timer settings of this user.

-R: Delete the timer settings of this user.

-U <用户名称> Specifies the user name to set the timer.




Crontab format


Basic Format:

* *** Command

Hour, day, month, and week commands



The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.

The first column indicates the hour 1 ~ 23 (0 indicates 0 points)

The 3rd column indicates the date 1 ~ 31

The 4th column indicates the month 1 ~ 12

The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)

6th columns of commands to run



# Use the hash sign to prefix a comment

# + ------ Minute (0-59)

# | + ----- Hour (0-23)

# | + ---- Day of month (1-31)

# | + --- Month (1-12)

# | + -- Day of week (0-7) (Sunday = 0 or 7)

# |

# ***** Command to be executed




Some examples of crontab files:


30 21 ***/etc/init. d/nginx restart

Restart nginx at every night.



45 4, 10, 22 **/etc/init. d/nginx restart

Restart nginx at on the 1st, 10th, and 22th of every month.



10 1 ** 6, 0/etc/init. d/nginx restart

Restart nginx at every Saturday and Sunday.



0, 30 18-23 ***/etc/init. d/nginx restart

Restart nginx every 30 minutes from to every day.



0 23 ** 6/etc/init. d/nginx restart

Restart nginx at every Saturday.



**/1 ***/etc/init. d/nginx restart

Restart nginx every hour



* 23-7/1 ***/etc/init. d/nginx restart

Restart nginx every hour between PM and PM.



0 11 4 * mon-wed/etc/init. d/nginx restart

Restart nginx from every Monday to every Wednesday on the 4th of each month



0 4 1 jan */etc/init. d/nginx restart

Restart nginx at on January 1, January 1



*/30 */usr/sbin/ntpdate 210.72.145.20


Synchronization time every half hour


Apply to thinkphp
First, you must write the function code to be periodically executed into the Action. Or you can use a URL to access the website.
For example, I put the updated code that regularly traverses the Order every day to Order/update_status.
And my project access address is www.jieqinwang.com: This function of code execution path: http://www.jieqinwang.com/XXX/xxx
After obtaining this path, you can go to liunx to configure cron.

Crontab-e to go to the task list.
Press I to enter edit mode, and then add a task.
For example, I want to execute the code I just wrote at every day.
Add a new row 0 12 *** curl http://www.jieqinwang.com/XXX/xxx
Press Esc to exit the editing mode, and then enter: wq and press Enter. Save and exit.
In this way, a scheduled task is added. You can use crontab-l to check whether the task list is included.
The last step, because the addition may not take effect immediately. Restart the cron service.
Restart service command:/sbin/service crond restart
Success! If you have any questions, I will reply as soon as possible.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.