Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse crontab php automatic running

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to parse crontab php autorun. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Crontab is a command that comes with linux

The method of making php run automatically

There are many ways to run php automatically, which are divided into the following DZ and some methods done through the system and directly triggered to run the resident system.

There is a scheduled task in the Discuz background that allows php to run automatically.

The mechanism for DZ to schedule tasks is as follows:

1. First of all, when it is time to trigger the planned task, there are visits (members, tourists, spiders of the search engine) and then trigger the planned task to occur. Because PHP is the trigger is the language, no one visits him, he can do nothing. )

two。 Plan the execution of the task.

3. If the execution is successful, the information of the successful execution is returned and updated to the database to record the current execution time and the next execution time.

Currently, there are several ways to let php run automatically:

1: schedule tasks under windows

Using crantab under linux

Cons: must have server permissions

2: refresh a web page at regular intervals, such as using a js or php program.

Cons: the page must be left open with some tool.

3: triggered when a user accesses

Cons: triggers must be included in the page visited by the user.

Timed execution with crontab

It's an order from UNIX.

Crontab- operates on each user's daemon and the schedule for that execution.

Some of the specific parameters are as follows:

Crontab file [- u user]-replaces the current crontab with the specified file.

Crontab- [- u user]-replace the current crontab with standard input.

Crontab-1 [user]-lists the user's current crontab.

Crontab-e [user]-Editing the user's current crontab.

Crontab-d [user]-deletes the user's current crontab.

Crontab-c dir- specifies the directory of the crontab.

Crontab file format: M H D md cmd.

M: minutes (0-59).

H: hours (0-23).

D: days (1-31).

M: month (1-12).

D: the day of the week (0600 is Sunday).

Cmd to run the program, the program is sent to sh execution, this shell has only USER,HOME,SHELL these three environment variables.

Here is an example file:

The copy code is as follows:

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND

# every morning at 6: 00

106 * date

# every two hours

Zero zero date 2 *

# every two hours between 11:00 and 8 a.m., in the morning

023-7bat 2pr 8* date

# on the 4th of every month and 11:00 from Monday to Wednesday

0 11 4 * mon-wed date

# 4: 00 a. M. in January

0 4 1 jan* date

Example

The copy code is as follows:

Lark:~ > crontab-1 lists the user's current crontab.

# MIN HOUR DAY MONTH DAYOFWEEK COMMAND

10 6 * date

Zero zero date 2 *

023-7bat 2pm 8 * date

In linux, there are many ways to achieve timed operation, and the most flexible one should be crontab. When using crontab, you must pay special attention to the problem of environment variables. Here, take the sqlplus of oracle as an example to illustrate the use of crontab.

How to use 1:crontab: crontab [- e |-l |-r] File name-e: edit task-l: display task information-r: delete scheduled execution task information

File format processed in 2:crontab is minute, hour, date, month, week, file name * represents all conditions 5 * rem / home/oracle/execsql represents 5 minutes per hour execute / 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 you execute sqlplus under oracle, you must write it 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)

Execsql is required to be an executable program $ls-al execsql-rwxr-xr-x 1 oracle dba 374Oct 07 15:17 execsql

Using crontab, make the php program run at 12:00

Just change it according to the following format and add it to crontab.

00 * 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 authority, only the virtual space

There is a useful function in PHP. This is only gradually used in recent development. The function 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 set to True, it means that if the user stops running the script, it still does not affect the running of the script (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:

The copy 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); / / dormant for 1 minute

} while (true)

Unless the program is closed on the server, the broken code will be executed forever.

This is the end of this article on "how to parse crontab php autorun". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report