简体   繁体   中英

How can I make a scheduler in PHP without the help of cron

How can I make a scheduler in PHP without writing a cron script? Is there any standard solution?

Feature [For example]: sent remainder to all subscriber 24hrs b4 the subscription expires.

The standard solution is to use cron on Unix-like operating systems and Scheduled Tasks on Windows.

If you don't want to use cron , I suppose you could try to rig something up using at . But it is difficult to imagine a situation where cron is a problem but at is A-OK.

我看到的解决方案是一个循环(一段时间)或一个sleep(3600*24);

通过javascript在每个设置的时间间隔内通过发送ajax调用执行它

Please read my final opinion at the bottom before rushing to implement.


Cron really is the best way to schedule things. It's simple, effective and widely available. Having said that, if cron is not available or you absolutely don't want to use it, two general approaches for a non-cron, Apache/PHP pseudo cron running on a traditional web server, is as follows.

Check using a loadable resource
Embed an image/script/stylesheet/ other somewhere on each web page. Images are probably the best supported by browsers (if javascript is turned off there's no guarantee that the browser will even load .js source files). This page will send headers and empty data back to the browser (a 1x1 clear .gif is fine - look at fpassthru)

from the php manual notes

<?php
header("Content-Length: 0");
header("Connection: close");
flush();

// browser should be disconnected at this point
// and you can do your "cron" work here
?>

Check on each page load
For each task you want to automate, you would create some sort of callable API - static OOP, function calls - whatever. On each request you check to see if there is any work to do for a given task. This is similar to the above except you don't use a separate URL for the script. This could mean that the page takes a long time to load while the work is being performed.

This would involve a select query to your database on either a task table that records the last time a task has run, or simply directly on the data in question, in your example, perhaps on a subscription table.

Final opinion
You really shouldn't reinvent the wheel on this if possible. Cron is very easy to set up .
However, even if you decide that, in your opinion, cron is not easy to set up, consider this: for each and every page load on your site, you will be incurring the overhead of checking to see what needs to be done. True cron, on the other hand, will execute command line PHP on the schedule you set up (hourly, etc) which means your server is running the task checking code much less frequently.

Biggest potential problem without true cron

You run the risk of not having enough traffic to your site to actually get updates happening frequently enough.

Create a table of cronjob. In which keep the dates of cron job. Keep a condition, if today date is equal to the date in the creonjob table. then call for a method to execute. This works fine like CRON job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM