About timed tasks in PHP crontab detailed

Source: Internet
Author: User
Recently encountered a lot about scheduled tasks, to be honest, the PHP script itself also has a two functions can be combined to make a timed task, but the effect is very general, the first choice is the system of task planning, whether it is win or Linux system has a task planning function, and we have to do is to use these features, The following is the Linux timing task in detail, because too long, directly find a copy, there is a need to see, we are using this, timed to execute our specific PHP files, nonsense not to say ~
Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways:
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
You can also start the service automatically when the system starts:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now cron This service is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use:
1. Edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a user's cron service
CRONTAB-E//Edit a user's cron service
For example, root to view your cron settings: Crontab-u root-l
Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e
Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
The first part of this format is the time setting, the next part is the command to execute, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can recall the full path of the command when the call. Time setting we have a certain agreement, the preceding five * number represents 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 on behalf of Sunday
In addition to the numbers there are several special symbols that are ' * ', '/' and '-', ', ', * represent all values within the range of numbers, '/' for each meaning, ' */5 ' represents every 5 units, '-' represents a number to a number, ', ' to separate several discrete numbers. Here are a few examples to illustrate the problem:
Every morning at 6.
0 6 * * echo ' Good morning. ' >>/tmp/test.txt//Note simply Echo, there is no output from the screen, because Cron will email any output to root's mailbox.
Every two hours
0 */2 * * * echo ' has a break now. ' >>/tmp/test.txt
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * * echo ' Have a good dream:) ' >>/tmp/test.txt
Every month, number 4th and Monday to Sunday, three a.m., 11.
0 4 * 1-3 command line
January 1 morning, 4.
0 4 1 1 * command line
After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.
2. Edit/etc/crontab File Configuration cron
Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=///user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly
Attention to the ' run-parts ' parameter, if you remove this parameter, you can write a script name to run, not the folder name.
--------------------------------------
Basic format:
* * * * * command
Time-sharing Weekly command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)
6th List of commands to run
Some examples of crontab files:
* * * * */usr/local/etc/rc.d/lighttpd restart
The above example shows a 21:30 restart lighttpd per night.
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 4:45 restart lighttpd per month for 1, 10, 22nd.
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 1:10 restart lighttpd per Saturday and Sunday.
0,30 18-23 * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that LIGHTTPD restarts every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that the LIGHTTPD is restarted every Saturday.
* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart LIGHTTPD every hour
* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart lighttpd every hour.
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month and 11-point restart lighttpd from Monday to Wednesday
0 4 1 Jan */usr/local/etc/rc.d/lighttpd restart
January 1 4-point restart LIGHTTPD

The above describes the PHP in the timing of the task crontab detailed, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.