About the use of crontab _php tips

Source: Internet
Author: User
Tags php script
Use Crontab to execute PHP code at timed intervals, for example, every 10 minutes:
/10 * * * * wget-q--sqider http://******
First, use PHP to execute scripts in crontab
Just like calling a normal shell script in crontab (specifically crontab usage), use a PHP program to invoke the PHP script.
The following myscript.php are performed every hour:
Copy Code code as follows:

# CRONTAB-E
* * * * * */usr/local/bin/php/home/john/myscript.php

/usr/local/bin/php is the path to the PHP program.
ii. using URLs to execute scripts in crontab
If your PHP script can be triggered by a URL, you can use Lynx or curl or wget to configure your crontab.
The following example uses a Lynx text browser to access URLs to execute PHP scripts every hour. The Lynx text browser opens the URL by default using the dialog method. However, like the following, we use the-dump option in the Lynx command line to convert the URL output to standard output.
Copy Code code as follows:

* * * * * * lynx-dump http://www.jb51.net/myscript.php

The following example uses the Curl access URL to execute a PHP script every 5 minutes. Curl defaults to display output in standard output. With the "curl-o" option, you can also dump the output of the script to a temporary file.
Copy Code code as follows:

*/5 * * * */usr/bin/curl-o temp.txt http://www.jb51.net/myscript.php

The following example uses the wget access URL to execute a PHP script every 10 minutes. The-q option indicates quiet mode. "-O Temp.txt" indicates that the output is sent to a temporary file.
Copy Code code as follows:

*/10 * * * */usr/bin/wget-q-o temp.txt http://www.jb51.net/myscript.php

Here's a detailed description of him:
Name:
Crontab
Use permissions:All Users
How to use:
crontab [-u user] File
crontab [-u user] {-l |-r | e}
Description:
Crontab is used to allow the user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -u user is the time table that specifies the user, as long as you have permission (for example, root) to specify the schedule for others. If you do not use-u user, it means setting your own schedule.
Parameters:
CRONTAB-E: Execute the text editor to set the time table, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variables to specify the use of the text editor (for example, Setenv VISUAL Joe)
Crontab-r: Deletes the current schedule table
Crontab-l: List the current schedule
crontab file [-u user]-replaces the current crontab with the specified files.
the format of the schedule table is as follows:
F1 F2 F3 f4 f5 program
Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. Program represents the programs to be executed.
When F1 is *, it means that the PROGRAM,F2 is executed every minute for *, and the rest of the program is executed every hour.
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to be performed from A to a B-hour, and the remainder of the analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, and the rest of the analogy
The user can also store all the settings in the file file, using crontab file to set the schedule.
Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS
In December, every day at 6 to 12, every 3 hours of the morning/usr/bin/backup are performed:
0 6-12/3 */usr/bin/backup
Send a letter to Alex@domain.name from Monday to Friday every 5:00:
0 * * 1-5 mail-s "HI" Alex@domain.name </tmp/maildata
Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "haha"
0-23/2 * * * echo "haha"
Note:
When the program is executed at the time you specify, the system will send you a letter showing the execution of the program, if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line blank
Example 2:
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * Date
#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date
#1月份日早上4点
0 4 1, * date
Example
$crontab-L lists the user's current crontab.
The function of the crontab command is to schedule the execution of some commands at certain intervals. In the/etc directory there is a crontab file, where there are some scheduling programs running on the system. Each user can establish their own scheduling crontab.
The crontab command has three different forms of command-line structure:
crontab [-u user] [file]
crontab [-u user] [-e|-l|-r]
Crontab-l-U [-e|-l|-r] the first command line, file is the name of the command file. If the file is specified on the command line, then the crontab command is executed, and the file is copied to the Crontabs directory, and if the file is not made on the command line, the crontab command accepts the command typed on the standard input (keyboard). And they are also stored in the crontab directory.

The role of the-r option in the command line is to delete the user-defined file crontab from the/usr/spool/cron/crontabs directory;
The function of the-l option in the command line is to display the contents of the user crontab file.
Use the command crontab-u user-e command to edit the cron (c) job for user users. A user edits a file to add or modify any job request.

Execute command crontab-u user-r to remove all cron jobs for the current user.
The assignments and their scheduled time are stored in the file/usr/spool/cron/crontabs/username. Username uses the username, and in the appropriate file, holds the command that the user wants to run. The results of the command execution, both standard output and error output, are sent to the user in the form of a message. Each request in the file must contain six domains separated by spaces and tabs. The first five fields can take an integer value, specify when to start work, and the sixth field is a string, called a command field, which includes commands for crontab dispatch execution.
the integer value range and meaning of the first fifth field are:
0~59-
1~23 means hours
1~31 said Day
1~12 represents the Month
0~6 said week (of which 0 said Sunday)
/usr/lib/cron/cron.allow says who can use the crontab command. If it is an empty file, it indicates that no user can schedule a job. If the file does not exist and there is another file/usr/lib/cron/cron.deny, only users who are not included in this file can use the crontab command. If it is an empty file, it indicates that any user can schedule a job. Two files exist at the same time Cron.allow first, if none exists, only super users can schedule jobs.

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.