Home Backend Development PHP Tutorial How to make PHP perform tasks regularly (with code)

How to make PHP perform tasks regularly (with code)

Jul 29, 2016 am 08:33 AM
php

This article mainly introduces how to make PHP perform tasks regularly. It has certain reference value. Interested friends can take a look. I hope it will be helpful to you!

If you use PHP to perform certain tasks regularly, you can have the following two methods:

1. crontab under linux, scheduled tasks under windows

2. Use PHP related functions

<span style="font-size: 14px;">set_time_limit(0);<br>ignore_user_abort(true);<br>//这里写一个死循环<br></span>
Copy after login

The first method is the most common. If you don’t have permission to crontab on the PHP server, you can also find a machine of your own to regularly crontab to request the server

The second method is less reliable. Just restart Apache. Have to revisit, fastcgi would be better.

Example: Create index.php and test.txt. The function is to overwrite and write a number to test.txt every second, and the number will increase. The index.php code is as follows:

<span style="font-size: 14px;"><?php<br>ignore_user_abort(true);<br>$num=0;<br>set_time_limit(0);<br>//ini_set('max_execution_time',0);   用这句也行,效果和set_time_limit(0)一样<br>do{<br>	file_put_contents('./test.txt',$num);<br>	$num++;<br>	sleep(1);<br>}while(true);<br></span>
Copy after login

After closing the browser, I found that the script can still be executed, and the number is still increasing.

The reason is that these two key functions are at work:

ignore_user_abort(true) regardless of whether the client closes the browser, the following code will be executed.

set_time_limit(0) Cancel the execution time of the php file. If there is no this function, the default php execution time is 30 seconds, which means that after 30 seconds, this file will say goodbay.

If you do not use these two functions, you need to modify php.ini, find the max_execution_time configuration item, change 30 to 0, and set it to 0 to never expire. Just restart the server.

For more related tutorials, please visit A complete set of video tutorials on PHP programming from entry to master

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles