Home Backend Development PHP Tutorial How to parse crontab php automatically run_PHP tutorial

How to parse crontab php automatically run_PHP tutorial

Jul 21, 2016 pm 03:03 PM
crontab linux php Order method yes of automatic Bring your own parse

crontab is a command that comes with linux
How to make php run automatically
There are many ways to run php automatically, here are the following DZ and some through the system Completed methods and methods that directly trigger the running of the resident system.
Discuz has a scheduled task in the background that can make php run automatically.
The mechanism of DZ scheduled tasks is as follows:
1. First, when it is time to trigger the scheduled task, there is a visit (member, visitor, search engine spider) and then the scheduled task is triggered to occur. (Because PHP is a language that triggers, and no one accesses it, it can't do anything.)
2. Scheduled task execution.
3. If the execution is successful, information about successful execution will be returned, and the current record will be updated in the database. Execution time, the next time it needs to be executed.
Currently there are the following methods to make php run automatically:
1: Use scheduled tasks under windows
Use crantab under linux
Disadvantages: You must have server permissions
2: Use a certain web page to refresh it every once in a while, such as using a js or php program.
Disadvantage: You must use a tool to open the page.
3: Triggered when a user visits
Disadvantage: The trigger program must be included in the page visited by the user.
Using crontab to schedule execution
is a UNIX command
crontab - operates the daemon for each user and the schedule of the execution.
The specific parameters are as follows:
crontab file [-u user]-Replace the current crontab with the specified file.
crontab-[-u user]-Replace the current crontab with standard input.
crontab-1[user]-List the user's current crontab.
crontab-e[user]-Edit the user's current crontab crontab.
crontab-d[user]-Delete the user's current crontab.
crontab-c dir-Specify the crontab directory.
The format of crontab file: M H D m d cmd.
M: minutes (0-59).
H: hour (0-23).
D: Day (1-31).
m: month (1-12).
d: Day of the week (0~6, 0 is Sunday).
The program to be run by cmd is sent to sh for execution. This shell only has three environment variables: USER, HOME, and SHELL.
The following is an example file:

Copy the code The code is as follows:

#MIN HOUR DAY MONTH DAYOFWEEK COMMAND
#Every morning at 6 o'clock
106* * * date
#Every two hours
0*/2* * * date
#11 pm to 8 am Every two hours, morning time
0 23-7/2, 8* * * date
#The 4th of each month and 11 a.m. from Monday to Wednesday of each week
0 11 4* mon-wed date
#4 o'clock in the morning on January day
0 4 1 jan* date

Example
Copy code The code is as follows:

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 implement scheduled operation. The most flexible one should be crontab. When using crontab, you must pay special attention to environment variables. Here, we take sqlplus executing Oracle as an example to explain 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 file format processed in crontab is minute, hour, date, month and week. The file name to be executed * represents all conditions 5 * * * * rem /home/oracle/execsql represents 5 minutes of each hour. When executing 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. If sqlplus is executed under 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 (execute @test1.ext file, the username/password of the database is test1/test1)

The program that requires execsql to be executable $ ls -al execsql -rwxr-xr-x 1 oracle dba 374 Oct 07 15:17 execsql
Use crontab to make the php program run at 12:00
Change it according to the following format and add it to the crontab
00 0 * * * cd /your program path;php your program name.php
This needs to be compiled with php in cli or cgi mode.
In addition:
If there is no administrative permission, there is only a virtual space
There is a function in PHP that is very useful. This is gradually used in recent development. int ignore_user_abort ([bool setting]) The function of this function is to instruct the server whether 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 running the script, it will not affect the running of the script (that is, the script will continue to execute); if set to False, it means that when the user stops running the script, the script will stop running. In the following example, after the user closes the browser, the script continues to execute on the server:

Copy the code The code is as follows:

ignore_user_abort(); // Run in the background
set_time_limit(0); // Cancel the timeout limit for script running time
do{
sleep(60); // Sleep for 1 minute
} while(true);

Unless this program is closed on the server, this code will continue to execute forever.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327820.htmlTechArticlecrontab is a command that comes with Linux to make php run automatically. There are many ways to run php automatically. Here are the following DZ and some methods completed through the system and direct triggers to run the resident system...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles