scheduled PHP script execution without cron

回眸只為那壹抹淺笑 提交于 2020-01-04 02:05:13

问题


I have a PHP script that needs to be executed by the end of every month. How would I do that without using cron on Unix-like systems or Task Scheduler on Windows systems? Isn't there some built-in feature in php for this? Because using cron/scheduler is not that much portable way IMO.


回答1:


Php is just a scripting language, not a process that is aware of time and may trigger events at desired moments.

Once you settle in a server, you don't have to worry about portability: migrating to another place won't be an automatic process because the environment could be very different.

So stick with cron, or write yourself a OS-agnostic layer.




回答2:


The only other way is by triggering it when the website is loaded. For example, you'd store the time the script was last run. Then every time a page is loaded, you'd check if it's time to run it. If so, then you would run it as part of the page load. Of course, this depends on (1) the task being a very quick one, and (2) that the task doesn't have to be run exactly on schedule, down to the second. This is basically how wp_cron() works in Wordpress.




回答3:


In the system i worked on, i had a little module that had the last running of the script saved, and on every running of the main script, it checked, if the script shouldn't have been run. In case it should have, it did before anything else executed, so the system had the right data anyway, even if the periodical script wasn't run "on time". It also checked if the script shouldn't have been run more than once, and ran it several times, if needed. A bit crude, but produces the right results without anything but PHP/mysql.



来源:https://stackoverflow.com/questions/12227335/scheduled-php-script-execution-without-cron

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!