Analytic crontab PHP Automatic running method _php skill

Source: Internet
Author: User
Tags sqlplus

Crontab is a Linux-led command.
Ways to make PHP run automatically
There are a number of ways to automate PHP, which are divided into the following DZ and some methods that are done through the system and directly trigger the operation of the resident system.
Discuz background has a scheduled task, you can make PHP run automatically.
the mechanism for DZ planning tasks is this:
1. First of all, when it is time to trigger a scheduled task, there is access (Member, visitor, search engine spider) and then triggers the scheduled task to occur. (Because PHP is the trigger is the language, no one to visit him, he can do nothing.) )
2. Plan the implementation of the task.
3. Successful execution, return the execution success information, update to the database to record the current execution time, the next time need to execute.
There are several ways to get PHP to run automatically at this time:
1: Use Scheduled Tasks under Windows
Use Crantab under Linux
Disadvantage: Must have server permissions
2: Use a Web page for a period of time to refresh, such as using JS or PHP program to achieve.
Disadvantage: The page must be open with a tool.
3: Triggered when a user accesses
disadvantage: The trigger must be included in the page that the user accesses.
timed execution with crontab
It's a UNIX command.
crontab-the daemon for each user and the schedule for the execution.
Some of the specific parameters are described below:
crontab file [-u user]-replaces the current crontab with the specified files.
Crontab-[-u user]-replaces the current crontab with standard input.
crontab-1[user]-lists the user's current crontab.
crontab-e[user]-Edit user's current crontab.
crontab-d[user]-deletes the user's current crontab.
Crontab-c dir-Specifies the directory for crontab.
crontab file format: M H d M D cmd.
M: Minutes (0-59).
H: Hours (0-23).
D: Days (1-31).
M: Month (1-12).
D: Days of the week (0~6,0 for Sunday).
CMD to run the program, the program was sent to SH execution, this shell only user,home,shell these three environment variables.
Here is an example file:

Copy Code code as follows:

#MIN HOUR Day MONTH DayOfWeek COMMAND
#每天早上6点
106* * * Date
#每两个小时
0*/2* * * Date
#晚上11点到早上8点之间每两个小时, Early Upper point
0 23-7/2,8* * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4* mon-wed Date
#1月份日早上4点
0 4 1 jan* date

Example
Copy Code code as follows:

LARK:~>CRONTAB-1 lists the user's current crontab.
#MIN HOUR Day MONTH DayOfWeek COMMAND
Ten 6* * * Date
0*/2* * * Date
0 23-7/2,8 * * * Date

In Linux, there are a variety of ways to achieve timed operation, the most flexible use should be crontab, in the use of crontab must pay special attention to environmental variables, here to perform the sqlplus of Oracle as an example, explain the use of crontab.

1:crontab Usage: crontab [e | l |-r] File name-e: Edit Task-L: Show task Information-r: Delete timed execution task information

The file format processed in 2:crontab is the name of the minute-hour date of the month week. * represents all Conditions 5 * * * * Rem/home/oracle/execsql represents 5 minutes per hour for execution/home/oracle/exec SQL file

3: Commands that need to run under specific environment variables, such as SQL, must be listed in the execution file. If you execute sqlplus under ORACLE, you must write in the following format: $ cat execsql oracle_home=/ora815;export oracle_home oracle_owner=oracle;export Oracle_owner oracle_sid=ora815;export oracle_sid oracle_base=/ora815/app/oralce;export ORACLE_BASE LD_LIBRARY_PATH= $ORACLE _home/lib;export ld_library_path path= $PATH: $ORACLE _home/bin: $LD _library_path;export PATH Nls_lang=american _america. Zhs16cgb231280;export nls_lang/ora815/bin/sqlplus test1/test1 @test1. ext (Execute @test1.ext file, database username/password is test1/test1)

which requires execsql to be an executable program $ ls-al execsql-rwxr-xr-x 1 Oracle DBA 374 Oct 15:17
Use crontab to make the PHP program run at 12:00
Just follow the format and add it to the crontab.
0 * * * * CD/Your program path; php your program name. php
This needs to be compiled with the CLI or CGI mode of PHP.
Other than that:
Without administrative privileges, only virtual space
There is a function in PHP that is useful. This is only gradually used in recent development. int Ignore_user_abort ([bool setting]) This function is to instruct the server side to continue executing the following script after the remote client closes the connection. The setting parameter is an optional parameter. If set to true, it means that if the user stops the script running, it still does not affect the script's running (that is, the script will continue to execute); If set to false, it means that the script will stop running when the user stops running the script. In the following example, after the user closes the browser, the script continues to execute on the server:

Copy Code code as follows:

Ignore_user_abort (); Background run
Set_time_limit (0); Timeout limit for canceling script run time
do{
Sleep (60); Sleep for 1 minutes
}while (TRUE);

Unless you shut down the program on the server, the code will be executed forever.

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.