简体   繁体   English

php time_sleep_until()和“Mysql服务器已经消失”

[英]php time_sleep_until() and “Mysql server has gone away”

i have script which must execute after every n minutes. 我有脚本,必须在每n分钟后执行一次。 n minutes is dynamic so that i could not set a cron job to call the script (at a specific time). n分钟是动态的,因此我无法设置cron作业来调用脚本(在特定时间)。

so what i did was i stored the time after every n minutes in an array so that when the script is executed, it will first check whether the current time is in the array. 所以我所做的是在数组中每n分钟后存储一次,这样当脚本执行时,它将首先检查当前时间是否在数组中。 if it is found in the array, it continues to executes otherwise it exits. 如果在数组中找到它,它将继续执行,否则退出。

to execute the script, i must use a cron job to run every minute to check the time in the array. 要执行脚本,我必须使用cron作业每分钟运行一次以检查数组中的时间。 unfortunately, my web host only allows 5 minutes as the least interval. 不幸的是,我的网络主机只允许5分钟作为最小间隔。 so every time the script is called, i check whether the values between $current_time and $current_time + (4*60) // 4 minutes is found in the array. 所以每次调用脚本时,我都会检查$current_time$current_time + (4*60) // 4 minutes之间的值是否在数组中找到。 if it is, and if needed, i use time_sleep_until to delay the script until the time reaches the value found in the array. 如果是,如果需要,我使用time_sleep_until来延迟脚本,直到时间达到数组中找到的值。

so if my script executes at 10:05 and the value found in the array is 10:06, i let the script sleep until 10:06 before it continues to execute. 因此,如果我的脚本在10:05执行并且数组中找到的值是10:06,我让脚本一直睡到10:06才继续执行。 however, if the sleep time is more than a minute or so, i get a Mysql server gone away . 但是,如果睡眠时间超过一分钟左右,我就会让Mysql server gone away

how can i prevent this? 我怎么能阻止这个? or is there a better way to do this? 或者有更好的方法吗?

thanks! 谢谢!

A couple choices, which is better I do not know. 几个选择,哪个更好,我不知道。

One, is make sure your script works with CLI and after that minute is up call it with the http://www.php.net/exec function (if your host allows it). 一,确保你的脚本适用于CLI,并在那之后用http://www.php.net/exec函数调用它(如果你的主机允许的话)。

Two, is setup a script, with a possible hash as a key and use a header redirect after the minute is up, this would call the script brand new so a new MySQL connection is made. 二,设置一个脚本,可能的哈希作为键,并在分钟结束后使用头重定向,这将调用脚本全新,以便建立新的MySQL连接。

A third option is to set the script up like in two, except setup a schedule task / cron job on your computer that opens that page (it would have to be in the webroot) and calls it every minute or however you want. 第三个选项是将脚本设置为两个,除了在计算机上设置打开该页面的计划任务/计算机作业(它必须在webroot中),并且每分钟调用一次或者根据需要调用它。 This is not a set method, but depends on how much your computer is on. 这不是一种设置方法,而是取决于您的计算机的数量。

Fourth, similar to the third but use a free cron job hosting service like: http://www.onlinecronjobs.com/en 第四,类似于第三,但使用免费的cron工作托管服务,如: http//www.onlinecronjobs.com/en

Hope that helps. 希望有所帮助。 If I think of other options I will update. 如果我想到其他选项,我会更新。

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

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