PHP定时执行任务/Cron Job
对于PHP本身并没有一套解决方案来执行定时任务,不过是借助sleep函数完成的。这种方就是要提前做一些配置,如实现过程:
<span ignore_user_abort</span>();<span //</span><span 关掉浏览器,PHP脚本也可以继续执行.</span> <span set_time_limit</span>(0);<span //</span><span 通过set_time_limit(0)可以让程序无限制的执行下去</span> <span $interval</span>=60*30;<span //</span><span 每隔半小时运行</span> <span do</span><span { </span><span //</span><span 这里是你要执行的代码 </span> <span sleep</span>(<span $interval</span>);<span //</span><span 等待5分钟</span> }<span while</span>(<span true</span>);
不过关于这种方式我有一些担心性能方面的问题,不过也是个临时的方法。
本人推荐的方式采用脚本来实现,利用OS本身的定时任务机制,windows上场利用bat脚本。不过本人没有在window上尝试过。那我就讲讲在linux中的实现。
如果你的web server是基于linux的,那么可以采用linux下的cron job 来完成。以RedHat5 为例,我们只需要预先要定时执行的逻辑代码。例如demo.php
<?<span php </span><span echo</span> "Hello"<span ; </span>?>
然后是吧php用shell脚本一封装,在shell脚本中调用demo.php,demo.sh代码如下:
#!/bin/<span bash </span><span #if</span> you php install to /usr/local/php/ /usr/local/php/bin/php /home/xx-user/demo.php
完成shell脚本的编写之后,确保它有足够的这行权限,例如:/bin/chmod u+x demo.sh.
然后配置linux上的cronjob,cronjob是linux上默认安装的。如果你的任务是需要按小时、天、周、月来执行的话,那么你直接可以将你的demo.sh脚本拷贝到
/etc/cron.hourly 、/etc/cron.daily 、/etc/cron.weekly、/etc/cron.monthly 就ok了,这样就可以完成你的任务了 。如果到某个时间点想移除定时任务,那就从上面这些folder中move到其它地方或者直接删除掉就ok了。
如果你的脚本有特殊的执行时间,例如每个星期的周二,或者是每个月的15号执行。那么你就需要配置属于你自己的cron job.
关于cron的特殊的配置请参考:http://www.pantz.org/software/cron/croninfo.html
这里我就按照每天的上午12点到下午的14点钟之间每2分钟运行一次这个脚本,那么配置如下(例如demo.sh是位于/tmp 目录下):
首先在linux的命令行中执行crontab -e ,然后把规则输入到里面:
*/<span 2</span> <span 12</span>-<span 14</span> * * * /tmp/demo.<span sh</span>
带输入完成之后按键盘上的“Esc”键,然后输入:wq,编辑页面就退出了。然后你可以用crontab -l 来查看你刚编辑的cron job.
到此时特殊的cron 也就完成了。例如你刚才是用linux下的demo账户来完成上面的步骤,那么还有一个简单的方法就是直接可以编辑/var/spool/cron/demo 这个文件,可以直接
修改你的cron job. 例如 :vi /var/spool/cron/demo
利用OS的方式来管理你的定时任务是很快捷的,而且不用你担心性能的问题,除非你的脚本本身存在一些问题。这样的方式易于维护,可以修改定时执行的计划,也可以轻松的移除和新增其它的定时任务。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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

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

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

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,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
