Parse crontab php automatic running method

  • 2020-06-19 09:55:28
  • OfStack

crontab is a command that comes with linux
A way to make php run automatically
There are many ways to automate php, including the following DZ and one that is done through the system and one that directly triggers the run-resident system.
Discuz has a scheduling task in the background that makes php run automatically.
The mechanism of the DZ task planning is as follows:
1. First, when it is time to trigger the scheduled task, there are visits (members, tourists, spiders of the search engine) and then trigger the scheduled task to happen. (Because PHP is a trigger and a language, no one visits him and he can do nothing.)
2. Plan for task execution.
3. Successful execution: the information of successful execution is returned and updated to the database to record the current execution time and the time needed for the next execution.
There are several ways to get php to run automatically:
1. Plan tasks under windows
Use crantab under linux
Cons: You must have server permissions
2: Use a certain page to refresh every 1 period, such as js or php program to achieve.
Cons: Some tool must be used to leave the page open.
3: Triggered when a user accesses it
Cons: The trigger must be included in the page the user visits.
Scheduled execution with crontab
Is one command of UNIX
crontab- Operates the daemon for each user and the schedule for this execution.
Specific partial parameters are explained as follows:
crontab file [-ES41en user]- replaces the current crontab with the specified file.
crontab-[-ES46en user]- replace the current crontab with standard inputs.
crontab-1 [user]- lists the user's current crontab.
crontab-e[user]- edit the user's current crontab.
crontab-d [user]- Delete the user's current crontab.
crontab-c dir- Specifies the directory of crontab.
crontab file format: M H D m d cmd.
M: minutes (0-59).
H: Hours (0-23).
D: Day (1-31).
m: Month (1-12).
Days of the week (0-6, 0 being Sunday).
cmd to run the program, the program is sent to sh to execute, this shell only USER,HOME,SHELL these three environment variables.
Here is an example file:


#MIN HOUR DAY MONTH DAYOFWEEK COMMAND 
# Every day in the morning 6 point  
106* * * date 
# Every two hours  
0*/2* * * date 
# evening 11 Point to the morning 8 Between the dots every two hours, the first dot in the morning  
0 23-7/2 . 8* * * date 
# Each month 4 Number and the weekly service 1 To worship 3 In the morning 11 point  
0 11 4* mon-wed date 
#1 Month day morning 4 point  
0 4 1 jan* date 

sample

lark:~>crontab-1  Lists the user's current crontab. 
#MIN HOUR DAY MONTH DAYOFWEEK COMMAND 
10 6* * * date 
0*/2* * * date 
0 23-7/2,8 * * * date

In linux, there are many ways to realize regular running, and the most flexible one should be crontab. Special attention must be paid to environment variables when using crontab. Here, sqlplus executing oracle is taken as an example to illustrate the usage of crontab.

crontab [-e |-l |-r] filename -e: edit task-l: display task-r: delete timed task information

2: The file format processed in crontab is minutes, hours, days, months and weeks. The file name * represents all conditions 5 * * * rem /home/oracle/execsql represents 5 minutes per hour to execute the /home/oracle/execsql file

3: Commands such as sql that need to be run under specific environment variables must be listed in the execution file. $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 (execution @ test1 ext files, database user name/password for test1 / test1)

It requires execsql to be an executable program $ls-ES203en ES204en-ES206en-ES207en 1 oracle dba 374 Oct 07 15:17 execsql
Use crontab to make the php program run at 12:00
Just follow the format below and add it to crontab
00 0 * * * cd/your program path; php Your program name.php
This needs to be compiled with cli or php mode.
In addition:
Only virtual Spaces are available without administrative permissions
There's a useful function in PHP. This is something that has only evolved in recent development. int ignore_user_abort ([bool setting]) this function indicates whether the server side should continue executing the following script after the remote client closes the connection. The setting parameter is an optional parameter. If it is set to True, it means that if the user stops the script running, it still does not affect the running of the script (that is, the script will continue to execute); If set to False, the script will stop running when the user stops running the script. In the following example, the script continues to execute on the server after the user closes the browser:


 ignore_user_abort(); //  The background 
 set_time_limit(0); //  Remove the timeout limit for the script run time 
 do{
 sleep(60); //  dormancy 1 minutes 
 }while(true);

Unless the program is shut down on the server, the code will run forever.


Related articles: