简体   繁体   English

PHP 没有 Cron 的计划任务

[英]PHP scheduled task without Cron

I have shared hosting with limited privileges to make advanced setup.我以有限的权限共享主机以进行高级设置。 But I need to make some job once a time without Cron.但是我需要在没有 Cron 的情况下一次完成一些工作。 So how can I run PHP script at scheduled time?那么如何在预定时间运行 PHP 脚本呢? Let's say I need to send email notification from server after 2h.假设我需要在 2 小时后从服务器发送 email 通知。 My first idea to make loop until time is up, but wouldn't it drain resources unnecessarily?我的第一个想法是循环直到时间结束,但它不会不必要地消耗资源吗? Second just compare server system time with desired time, but has nothing to run the script.其次只是将服务器系统时间与所需时间进行比较,但没有什么可以运行脚本。 Any idea how to solve this issue?知道如何解决这个问题吗?

Take a look at the PHP sleep function ( https://www.php.net/manual/en/function.sleep.php ) Take a look at the PHP sleep function ( https://www.php.net/manual/en/function.sleep.php )

You could potentially have a loop that would compare the current time to your desired time.您可能有一个循环,将当前时间与您想要的时间进行比较。 If the current time still wasn't your desired time, you could sleep for x number of seconds.如果当前时间仍然不是您想要的时间,您可以睡 x 秒。

*Word of caution: This method likely won't get your script to run at exactly your desired time like a cron job, but will be accurate to the amount of time you put in your sleep call in-between checks. *注意事项:这种方法可能不会像 cron 作业那样让您的脚本在您想要的时间运行,但会准确到您在两次检查之间进行睡眠呼叫的时间量。 Also, I wouldn't trust the sleep function to return at exactly your desired time (ex. if you want to wait five minutes then sleep(5*60) likely wouldn't return at exactly 5 minutes. Cron jobs would definitely be recommended if possible. *另外,我不相信 sleep function 会在您想要的时间返回(例如,如果您想等待 5 分钟,则sleep(5*60)可能不会在 5 分钟后返回。肯定会推荐 Cron 作业如果可能的话。 *

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

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