What are the methods to implement scheduled tasks in PHP?

王林
Release: 2023-03-07 15:10:01
Original
2360 people have browsed it

php methods to implement scheduled tasks are: 1. Use the crontab under the Linux system to implement; 2. Use the ignore_user_abort function to implement; 3. Use the file_get_contents function to implement.

What are the methods to implement scheduled tasks in PHP?

1. Server scheduled tasks

Server scheduled tasks are actually under unix systems crontab implementation, specific settings: Linux scheduled task crontab; however, in addition to directly reading PHP scripts regularly, we can also use scheduled interface calls.

(Learning video recommendation:java video tutorial)

When running the script:

*/1 * * * * php /data/www/cron.php Execute cron.php every minute

URL mode call:

lynx mode:*/1 * * * * lynx -dump http://www. xxxxxxx.com/cron.php (-dump option to convert the URL output to standard output)

curl method: */1 * * * * /usr/bin/curl -o temp.txt http: //www.xxxxxxx.com/cron.php (Curl displays output on standard output by default. Use the "curl -o" option to dump the output of the script to the temporary file temp.txt)

wget method: */1 * * * * /usr/bin/wget -q -O temp.txt http://www.xxxxxxx.com/cron.php (the q option indicates quiet mode. "-O temp.txt" indicates that the output will Send to a temporary file.)

The above method is to place the php script in an address that can be accessed using a URL, such as http://www.xxxxxx.com/cron.php, and call the trigger task regularly.

2. ignore_user_abort() method

ignore_user_abort() function sets whether disconnecting from the client will terminate the execution of the script.

First use a cron.php file to control the termination of the script. The content of cron.php is:

Copy after login

The script php file is:


        
Copy after login

By changing the cron.php return 0 , to stop the program. But this method is not good at memory management.

3. file_get_contents() method

Copy after login

php script sleep continues to execute after a period of time by accessing itself. This ensures that the execution time of each PHP script will not be too long and will not be affected by time_out limit. Because each loop of the php file is executed independently, this method avoids the time_out limit. But it is best to add the control code cron.php as above so that the process can be terminated.

Related recommendations:php training

The above is the detailed content of What are the methods to implement scheduled tasks in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!