This article provides a detailed analysis of the use of crontab. For more information about how to use crontab to regularly execute php code, for example, once every 10 minutes:
/10 ***** wget-q -- sqider http ://******
1. use PHP to execute scripts in Crontab Just like calling a common shell script in Crontab (specific Crontab usage), you can use a PHP program to call the PHP script.
Execute myscript. php every hour as follows:
The code is as follows:
# Crontab-e
00 ***/usr/local/bin/php/home/john/myscript. php
/Usr/local/bin/php is the path of the PHP program.
2. use the URL in Crontab to execute the script If your PHP script can be triggered through a URL, you can use lynx, curl, or wget to configure your Crontab.
The following example uses the Lynx text browser to access the URL and execute the PHP script hourly. By default, the Lynx text browser opens a URL in dialog mode. However, as shown below, we use the-dump option in the lynx command line to convert URL output to standard output.
The code is as follows:
00 *** lynx-dump http://www.bitsCN.com/myscript.php
The following example uses CURL to access the URL and execute the PHP script every 5 minutes. Curl displays the output in standard output by default. With the "curl-o" option, you can also dump the script output to a temporary file.
The code is as follows:
*/5 */usr/bin/curl-o temp.txt http://www.bitsCN.com/myscript.php
The following example uses the wget url to execute the PHP script every 10 minutes. -The q option indicates quiet mode ." -O temp.txt "indicates that the output will be sent to a temporary file.
The code is as follows:
*/10 ***/usr/bin/wget-q-O temp.txt http://www.bitsCN.com/myscript.php
Let's give a detailed introduction to him: Name:Crontab
Permission:All users
Usage: Crontab [-u user] file
Crontab [-u user] {-l |-r |-e}
Note: Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to set the time table of the specified user. The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, the time table is set.
Parameters: Crontab-e: execute the text editor to set the time table. the preset text editor is VI. if you want to use another text editor, set the VISUAL environment variable to specify the text editor (for example, setenv VISUAL joe)
Crontab-r: delete the current time table
Crontab-l: to list the current time table
Crontab file [-u user]-replace the current crontab with the specified file.
The time table format is as follows: F1 f2 f3 f4 f5 program
F1 indicates the minute, f2 indicates the hour, f3 indicates the day of the month, f4 indicates the month, and f5 indicates the day of the week. Program indicates the program to be executed.
When f1 is *, the program is executed every minute. when f2 is *, the program is executed every hour, and so on.
When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on
When f1 is */n, it indicates execution is performed every n minutes. if f2 is */n, it indicates execution is performed every n hours, and so on.
When f1 is a, B, c ,... a, B, c ,... execute in minutes. f2 is a, B, c ,... a, B, c... execution in hours, and so on
You can also store all settings in the file first, and use crontab file to set the time table.
Example: # Run/bin/ls at every morning:
0 7 ***/bin/ls
During October 11, December, execute/usr/bin/backup every three hours from to every day:
0 6-12/3*12 */usr/bin/backup
From Monday to Friday, send a letter to alex@domain.name at PM:
0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata
Execute echo "haha" at midnight, 00:20, and 02:20 every month"
20 0-23/2 *** echo "haha"
Note: When the program is executed at the specified time, the system will send you a letter showing the program execution content. if you do not want to receive such a letter, add>/dev/null 2> & 1 after each line is empty.
Example 2: #06:10 every morning
10 6 **** date
# Every two hours
0 */2 * date
# Every two hours from PM to AM, AM
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * mon-wed date
# A.m. of July
0 4 1 jan * date
Example $ Crontab-l list the current crontab of a user.
The crontab command schedules the execution of some commands at certain intervals. There is a crontab file in the/etc directory, which stores some scheduling programs that are running systematically. Each user can create their own scheduling crontab.
The crontab command has three forms of command line structure: Crontab [-u user] [file]
Crontab [-u user] [-e |-l |-r]
Crontab-l-u [-e |-l |-r] in the first command line, file is the name of the command file. If this file is specified in the command line, run the crontab command to copy the file to the crontabs Directory. If this file is not specified in the command line, the crontab command will accept the commands typed on the standard input (keyboard) and store them in the crontab directory.
In the command line, the-r option is used to delete the crontab file from the/usr/spool/cron/crontabs directory;
In the command line, The-l option is used to display the contents of the crontab file.
Run the crontab-u user-e command to edit the cron (c) job of the user. You can edit a file to add or modify any job requests.
Run the crontab-u user-r command to delete all cron jobs of the current user.
Jobs and their scheduled time are stored in the file/usr/spool/cron/crontabs/username. Username stores the user name in the corresponding file with the command that the user wants to run. The command execution result, whether it is standard output or error output, will be sent to the user in the form of mail. Each request in the file must contain six fields separated by spaces and tabs. The first five fields can take an integer to specify when to start working. The Sixth field is a string called a command field, which includes the crontab command for scheduling and execution.
The value range and significance of the integer in the first and fifth fields are as follows: 0 ~ 59 indicates the score
1 ~ 23 indicates the hour
1 ~ 31 indicates the day
1 ~ 12 indicates the month.
0 ~ 6 indicates the week (0 indicates Sunday)
/Usr/lib/cron. allow indicates who can use the crontab command. If it is an empty file, it indicates that no user can schedule a job. If this file does not exist and another file/usr/lib/cron. deny exists, the crontab command can be used only by users not included in this file. If it is an empty file, it indicates that any user can arrange jobs. If both files exist at the same time, cron. allow takes priority. if neither of them exists, only the super user can schedule a job.
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.