I wrote an article a few days ago, using simple php to implement scheduled task execution, but the efficiency is not good. for linux, it is more reasonable to use crontab. First, let's talk about cron, it is a scheduled execution tool in linux. Users other than root users can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the/var/spool/cron directory and executed using the user identity to create them. Create a crontab project as a user, log on as the user, and enter the crontab-e command to edit the crontab of the user. The format of the file is the same as that of/etc/crontab. After the changes made to crontab are saved, the crontab file is saved according to the user name and written to the file/var/spool/cron/username. The cron daemon checks the/etc/crontab file, the etc/cron. d/directory, and the changes in the/var/spool/cron directory every minute. If changes are found, they are loaded into the memory. In this way, you do not have to restart the Daemon after a crontab file is changed.
Install crontab:
Yum install crontabs
Note:
/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:
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:
Minute Hour day month week Command
******
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
Remember the meanings of several special symbols:
"*" Indicates the number in the value range,
"/" Indicates "every ",
"-" Indicates a number to a number,
"," Separate several discrete numbers
# 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
Example of crontab:
(1) first example.
30 21 ***/etc/init. d/nginx restart
Restart nginx at every night.
(2) The second example is the example tested in this tutorial.
* ***/Usr/bin/php-f/root/test. php> test. log
Run the/root/test. php file every minute and output the result to test. log.
After completing the above basic work, let's see how to use crontab to regularly execute the PHP script:
(1) I create the test. php file under/root. the content is as follows:
The code is as follows:
#! /Usr/bin/php-q
Echo date ('Y-m-d H: I: s'). "from http://www.phpddt.com \ n ";
?>
Note: You can use whereis php to find the location of the php execution file.
(2) then write the following shell in crontab-e:
The code is as follows:
* ***/Usr/bin/php-f/root/test. php> test. log
Note: test. php must be an executable file: chmod + x test. php
The test results are normal as follows:
Of course, you can use crontab-e to continue adding tasks. under/var/spool/cron, you can see a root file.
In windows, you can directly use windows to schedule tasks and open the webpage through bat. Unlike linux replication.