简体   繁体   English

php脚本每天运行几次,没有CRON?

[英]Php script running few times a day with no CRON?

Hey! 嘿! i'm looking for some way that can help me in task like this: write a function that every 2 days clear content of XML file. 我正在寻找某种可以帮助我完成此类任务的方法:编写一个每两天清除一次XML文件内容的函数。 Do this with no CRON or similar tools, just PHP. 无需CRON或类似的工具,只需使用PHP即可完成此操作。 So far i've never done time-related-run script so is there a way to make it with only PHP? 到目前为止,我还从未完成过与时间相关的运行脚本,所以有没有办法仅使用PHP? I assume that everything what functions does is without users ingerention, so i try function sleep(), but i think there must be better solution. 我认为函数所做的所有事情都没有用户的理解,因此我尝试使用函数sleep(),但我认为必须有更好的解决方案。 Anyone know way to help me? 有人知道如何帮助我吗?

On every request check modification or creation date of file. 根据每个请求,检查文件的修改或创建日期。 If current time minus file time is greater than 2 days - delete file or clear content. 如果当前时间减去文件时间大于2天-删除文件或清除内容。

Request in this case is poor resolution clock. 在这种情况下,请求的分辨率时钟较差。 Generally checking file time (stat) is quite fast operation. 通常,检查文件时间(状态)是相当快的操作。

There are services that call your scripts at given times. 有些服务会在给定的时间调用您的脚本。

ONLINE CRON SERVICES 在线CRON服务

You might want to look into running your script as a daemon. 您可能需要考虑将脚本作为守护程序运行。 PEAR's System_Daemon class manages a lot of this functionality. PEAR的System_Daemon类管理许多此类功能。 You can take a look at http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/ for a good tutorial on how to set up a daemon. 您可以查看http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/,以获得有关如何设置守护程序的良好教程。

Cron is obviously better for a timed task, but if cron really isn't available, this might be an option. 对于定时任务,Cron显然更好,但是如果cron确实不可用,则可以选择。

You can schedule the jobs using a MySQL database or a file as described here . 您可以按照此处所述使用MySQL数据库或文件调度作业。

WordPress is also using the same method when cron jobs doesn't available or not been configured. 当cron作业不可用或未配置时,WordPress也使用相同的方法。

如果没有Cron或不打开浏览器,我不确定是否可以?

Letting the script sleep() for 2 days is no good idea. 让脚本sleep()停留2天并不是一个好主意。 Use a webcron . 使用Webcron

如果没有cronjobs的原因是,您无法在服务器上定义cronjobs,也许您可​​以在另一台(本地)计算机上调用一个脚本,该脚本每隔一天发出一次HTTP请求,从而触发您的PHP脚本?

If your script is fast to execute you can use an "user based cron". 如果脚本可以快速执行,则可以使用“基于用户的cron”。

-You save somwhere the last execution time -Each visitor trigger a check on the last execution time -if the execution time exceed the defined duration the script is executed -在最后执行时间保存您的位置-每个访问者都会触发对最后执行时间的检查-如果执行时间超过了定义的持续时间,则执行脚本

It can be a good solution if your script don't need an exact execution timing and is very quick to execute (the user which trigger the execution should not be impacted) 如果您的脚本不需要确切的执行时间并且执行速度非常快(触发执行的用户不会受到影响),那么这可能是一个很好的解决方案

这将每2天(或指定天数)为您运行一次脚本http://www.phpjobscheduler.co.uk/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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