Several methods of implementing timing tasks in PHP

In the past few days, I need to use PHP to write a server application that regularly crawls web pages. I searched for the solution on the Internet, and found that the answer to a question from OSchina is very wonderful (it is worth watching, thank you for your wonderful answers O(∩_∩ )O~), put forward several solutions. It is summarized as follows.

1. Simple and direct reckless type
<?php
ignore_user_abort();//Close the browser, the PHP script can continue to execute.
set_time_limit(0);// Through set_time_limit(0), the program can be executed without limit
ini_set('memory_limit','512M'); // set memory limit
$interval=60*30;// run
do{
//ToDo 
sleep( $interval);//Wait for 5 minutes
}
while(true);
Disadvantage: After startup, you can't control it, unless you terminate the PHP host. Don't use this method, unless you are a hacker.

2. Simple controllable
config.php

<? php
return 1;
?>
cron.php



ignore_user_abort();//Close the browser, and the PHP script can continue to execute.
set_time_limit(0);// Through set_time_limit(0), the program can be executed without limit
$interval= 60*30;// run
do{ every half hour
$run = include 'config.php';
if(!$run) die('process abort');

//ToDo
sleep($interval);// wait for 5 minutes
}
while(true);
by changing the config.php return 0 , to stop the program. A feasible way is to interact with the config.php file and a special form, and configure some variables through the HTML page.

Disadvantages: occupying system resources, running for a long time, there will be some unexpected hidden dangers . Such as memory management issues.

3. Simple improvement
<?php
$time=15;
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
/*
function
* /
sleep($time);
file_get_contents($url);
?>
The php script sleep will continue to execute by accessing itself after a period of time. It is like a relay race.. This ensures that each PHP script execution time will not be too long . It is not limited by time_out.

Because each cycle of php files is executed independently, this method avoids the limitation of time_out. But it is best to add control code as above. cofig.php , so that you can Terminate the process.

4. Server timing tasks
Unix platform

If you're on a Unix system, you need to prepend your PHP script with a special line of code that makes it executable so the system knows what program to use to run the script. The first line of code added for Unix systems does not affect the script's running under Windows, so you can also write cross-platform scripts this way.

1. Using PHP to execute scripts
in Crontab is like calling ordinary shell scripts in Crontab (specific Crontab usage), using PHP programs to call PHP scripts, and executing myscript.php every hour as follows:

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php
/usr/local/bin/php is the path of the PHP program.

2. Execute scripts using URLs in Crontab
If your PHP scripts can be triggered by URLs, you can use lynx or curl or wget to configure your Crontab.

The following example uses the Lynx text browser to access a URL to execute a PHP script every hour. The Lynx text browser opens URLs in conversational mode by default. However, as below, we use the -dump option on the lynx command line to convert the URL output to standard output.

00 * * * * lynx -dump http://www.sf.net/myscript.php
The example below uses CURL to access the URL to execute a PHP script every 5 minutes. Curl displays output on standard output by default. Using the "curl -o" option, you can also dump the output of the script to a temporary file temp.txt.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.sf.net/myscript.php
The example below uses WGET to access the URL to execute a PHP script every 10 minutes. The -q option indicates quiet mode. "-O temp.txt" means that the output is sent to a temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.sf.net/myscript.php
5. Detailed explanation of ini_set function usage
PHP ini_set is used to set the value of php.ini, It takes effect when the function is executed, and the setting is invalid after the script ends. You can modify the configuration without opening the php.ini file, which is very convenient for virtual spaces.

Function format:

string ini_set(string $varname, string $newvalue)
Not all parameters can be configured, you can check the list in the manual.

Common settings:

@ ini_set('memory_limit', '64M');
menory_limit: Set the maximum number of bytes of memory that a script can apply for, which is helpful for poorly written scripts to consume the available memory on the server. The @ symbol means no errors are output.

@ini_set('display_errors', 1);
display_errors: Set the type of error information.

@ini_set('session.auto_start', 0);
session.

If the parameter is 0 and the session is not opened manually, an error will be reported.

@ini_set('session.cache_expire', 180);
session.cache_expire: Specifies the limited period (minutes) of the session page in the client cache. The default is 180 minutes. If session.cache_limiter=nocache is set, the setting here is invalid.

@ini_set('session.use_cookies', 1);
session.use_cookies: Whether to use cookies to save the session ID on the client side;

@ini_set('session.use_trans_sid', 0);
session.use_trans_sid: Whether to display the SID in the URL using plaintext (Session ID), the

default is forbidden, because it will bring security risks to your users:

users may tell others by email/irc/QQ/MSN etc. URLs containing valid sid.
A URL containing a valid sid may be saved on a public computer.
Users may save URLs with fixed SIDs in their favorites or browsing history. URL-based session management is always more risky than cookie-based session management and should be disabled.
(The most original blogger has not been found, so I can only declare it here, which is specially reproduced.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325133876&siteId=291194637