How to resolve crontabphp to run automatically

Source: Internet
Author: User
This article provides a detailed analysis of the methods for automatically running crontabphp. For more information, see Crontab is a built-in command in linux.
How to make php run automatically
There are many ways to run php automatically. here we will divide the following DZ and some methods completed through the system and the methods to directly trigger the running of the Resident system.
Discuz has a scheduled task in the background, which can make php run automatically.
DZ schedules tasks as follows:
1. when the scheduled task is triggered, access (members, visitors, search engine spider) is triggered and the scheduled task is triggered. (Because PHP is triggered by a language, no one can access it .)
2. scheduled task execution.
3. if the execution succeeds, information about successful execution is returned, and the current execution time is recorded in the database. The next execution time is required.
Currently, php can run automatically in the following ways:
1: Use scheduled tasks in windows
Use crantab in linux
Disadvantage: you must have Server permissions.
2: use a web page to refresh each time, for example, using js or php programs.
Disadvantage: you must use a tool to open the page.
3: triggered during user access
Disadvantage: the trigger program must be included in the page accessed by the user.
Scheduled execution with crontab
Is a UNIX command
Crontab-operate the Daemon of each user and the execution schedule.
The specific parameters are described as follows:
Crontab file [-u user]-replace the current crontab with the specified file.
Crontab-[-u user]-replace the current crontab with the standard input.
Crontab-1 [user]-list the current crontab of the user.
Crontab-e [user]-edit the current crontab of the user.
Crontab-d [user]-delete the current crontab of the user.
Crontab-c dir-specifies the crontab directory.
Crontab file format: m h d m d cmd.
M: minute (0-59 ).
H: Hour (0-23 ).
D: Day (1-31 ).
M: month (1-12 ).
D: days in a week (0 ~ 6, 0 is Sunday ).
The program to be run by cmd is sent to sh for execution. the shell contains only three environment variables: USER, HOME, and SHELL.
The following is an example File:

The code is as follows:


# MIN HOUR DAY MONTH DAYOFWEEK COMMAND
# Every morning
106 *** date
# Every two hours
0 */2 * date
# Every two hours from PM to 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

The code is as follows:


Lark: ~> The crontab-1 lists the current crontab of the user.
# MIN HOUR DAY MONTH DAYOFWEEK COMMAND
10 6 **** date
0 */2 * date
0 23-7/2, 8 **** date


In linux, there are multiple ways to implement scheduled operation. the most flexible way to use is crontab. when using crontab, you must pay special attention to the environment variables. Here we take the execution of oracle sqlplus as an example, describes how to use crontab.

1: crontab usage:Crontab [-e |-l |-r] file name-e: edit task-l: Display task information-r: delete scheduled execution task information

2: The files processed in crontab are in the format of minutes, hours, days, months, and weeks. The file name * represents all conditions ****Rem/home/oracle/execsql indicates that the/home/oracle/execsql file is executed every 5 minutes in an hour.

3: For SQL and other commands that need to be run under specific environment variables, they must be listed in the execution file.For example, if sqlplus is executed in oracle, it must be written 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)

Execsql is required to be an executable program $ ls-al execsql-rwxr-xr-x 1 oracle dba 374 Oct 07 execsql
Use crontab to run the php program
Just follow the format below and add it to crontab.
00 0 * cd/your program path; php your program name. php
This requires php in cli or cgi mode during compilation.
In addition:
If you do not have the management permission, you only have virtual space.
PHP has a function that is very useful. This is gradually used in recent development. Int ignore_user_abort ([bool setting]) indicates whether the server continues to execute the following script after the remote client closes the connection. The setting parameter is an optional parameter. If it is set to True, it indicates that if the user stops running the script, the script is still not affected (that is, the script will be executed continuously); if it is set to False, the script stops running when the user stops running the script program. In the following example, the script is still executed on the server after the user closes the browser:

The code is as follows:


Ignore_user_abort (); // run in the background
Set_time_limit (0); // cancel the upper limit of the script running time
Do {
Sleep (60); // sleep for 1 minute
} While (true );


Unless the program is closed 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.