程序员 - 如何定时执行一段PHP程序,不用linux或者windows上面的计划任务?

WBOY
Release: 2016-06-06 20:41:48
Original
901 people have browsed it

如何定时执行一段PHP程序,不用linux或者windows上面的计划任务?

回复内容:

如何定时执行一段PHP程序,不用linux或者windows上面的计划任务?

如果不用外部的定时器,那就只能用下面两个方法了
1,启动一个php进程一直在后台跑,做个定时器,检查有没有任务应该执行
2,通过用户在前台激发事件,有用户访问,则去检查有没有需要执行的任务

ps,这个蛋疼的需求在什么情况下才会遇到?

php页面的话(也就是可以通过web访问),可以用第三方的网页监控,最短五分钟一次。

不然我想不出来什么样的环境不能用crontab。

php不能常驻内存,除非你是在命令行里运行。所以说如果是web方式运行php,是实现不了这个功能的。

我们公司的奇葩做法,使用数字浏览器的自动刷新功能。。

<code>ignore_user_abort();
set_time_limit(0);
</code>
Copy after login

在程序中while(true)死循环并判断时间,到了就执行。
为了方便控制也可以读一个文件,如果文件为0就exit,这样可以通过修改文件内容来控制进程结束。

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
Popular Tutorials
More>
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!