Home > Backend Development > PHP Tutorial > Detailed explanation of scheduled task crontab in PHP

Detailed explanation of scheduled task crontab in PHP

WBOY
Release: 2016-07-28 08:27:49
Original
1637 people have browsed it

Recently I have encountered a lot of questions about scheduled tasks. To be honest, the PHP script itself has one or two functions that can be combined to create scheduled tasks, but the effect is very average. The first choice is to plan the system tasks better, whether it is win or Linux system. Task planning function, and what we have to do is to make good use of these functions. The following is a detailed explanation of scheduled tasks in Linux. Because it is too long, I directly found a copy. If you need it, you can take a look. We just use this to execute our tasks regularly. Let’s not talk too much about the specific PHP files~
cron is a scheduled execution tool under Linux that can run jobs without manual intervention. Since Cron is a built-in service of Linux, but it does not start automatically, you can use the following methods to start and shut down the service:
/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
You can also start this service automatically when the system starts:
In the /etc/rc.d/rc.local script Add at the end:
/sbin/service crond start
Now that the Cron service is already in the process, we can use this service. The Cron service provides the following interfaces for everyone to use:
1. Edit directly with the crontab command
The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
crontab -u //Set the cron service of a certain user. Generally, the root user needs this parameter when executing this command.
crontab -l //List the details of a certain user's cron service
crontab -r //Delete a certain user's cron service
crontab -e //Edit a certain user's cron service
For example, root can view his own cron Settings: crontab -u root -l
For another example, root wants to delete fred's cron settings: crontab -u fred -r
When editing the cron service, the edited content has some formats and conventions, enter: crontab -u root -e
Enter vi editing mode. The edited content must conform to the following format: */1 * * * * ls >> /tmp/ls.txt
The first part of this format is to set the time, and the latter part is If there are too many commands to be executed, you can write these commands into a script and then call the script directly here. Remember to write the full path of the command when calling. We have a certain agreement on setting the time. The first five * signs represent five numbers. The value range and meaning of the numbers are as follows:
minutes (0-59)
hours (0-23)
date (1-31)
Month (1-12)
Week (0-6) //0 represents Sunday
In addition to numbers, there are several special symbols: '*', '/' and '-', ',', * represents everything Numbers within the value range, '/' means every, '*/5' means every 5 units, '-' means from a certain number to a certain number, ',' separates several discrete numbers. Here are a few examples to illustrate the problem:
Every morning at 6 o'clock
0 6 * * * echo 'Good morning.' >> /tmp/test.txt //Note that with simple echo, no output can be seen on the screen. Because cron emails any output to the root mailbox.
Every two hours
0 */2 * * * echo 'Have a break now.' >> /tmp/test.txt
Every two hours between 11pm and 8am, 8am
0 23-7/2, 8 * * * echo 'Have a good dream:)' >> /tmp/test.txt
On the 4th of every month and at 11 a.m. from Monday to Wednesday every week Click
0 11 4 * 1-3 command line
At 4 a.m. on January 1st
0 4 1 1 * command line
Every time after editing a user's cron settings, cron will automatically be under /var/spool/cron Generate a file with the same name as this user. The cron information of this user is recorded in this file. This file cannot be edited directly and can only be edited with crontab -e. After cron starts, it reads this file every time and checks whether the commands in it need to be executed. Therefore, there is no need to restart the cron service after modifying this file.
2. Edit the /etc/crontab file to configure cron
The cron service not only needs to read all the files in /var/spool/cron once every minute, but also needs to read /etc/crontab once, so we can configure this file to use the cron service Do something. Configuration with crontab is for a certain user, while editing /etc/crontab is a task for the system. The file format of this file is:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root //If an error occurs or there is data output, the data will be sent as an email Send to this account
HOME=/ //The path where the user runs, this is the root directory
# run-parts
01 * * * * root run-parts /etc/cron.hourly //Execute /etc/cron every hour Scripts in .hourly
02 4 * * * root run-parts /etc/cron.daily //Execute scripts in /etc/cron.daily every day
22 4 * * 0 root run-parts /etc/cron.weekly //Execute the scripts in /etc/cron.weekly every week
42 4 1 * * root run-parts /etc/cron.monthly //Execute the scripts in /etc/cron.monthly every month
Please pay attention to 'run -parts' parameter. If you remove this parameter, you can later write the name of a script to be run instead of the folder name.
-------------------------------------------------
Basic format:
*  * * *  * command
Time-sharing, day, month, and week commands
The first column represents minutes 1 to 59. Each minute is represented by * or */1
The second column represents hours 1 to 23 (0 represents 0 o'clock)
The third column represents dates 1 to 31
Column 4 represents the month 1 to 12
Column 5 identifies the day of the week 0 to 6 (0 means Sunday)
Column 6 represents the command to be run
Some examples of crontab files:
30 21 * * * /usr/local/etc /rc.d/lighttpd restart
The above example indicates that lighttpd is restarted at 21:30 every night.
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
The above example indicates that lighttpd is restarted at 4:45 on the 1st, 10th, and 22nd of every month.
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The above example indicates that lighttpd is restarted at 1:10 every Saturday and Sunday.
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means restarting lighttpd every 30 minutes between 18:00 and 23:00 every day.
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
The above example indicates that lighttpd is restarted every Saturday at 11:00 pm.
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour between 11pm and 7am
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
On the 4th of every month and every Monday to Wednesday Restart lighttpd at 11 o'clock
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd at 4 o'clock on January 1st

The above has introduced a detailed explanation of the scheduled task crontab in PHP, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template