在特定时间自动安排php脚本执行 [英] automatically schedule a php script execution at specific time
问题描述
有什么想法可以做到这一点?我通过堆栈溢出来搜索并搜索,但是我遇到的一切都是在特定时间多次执行。我需要的是在自动设置的时间执行。
希望你帮我,我很绝望。
你有3个选项。我的建议:使用cron,如果你可以,用户驱动,如果你必须,守护进程作为最后的手段。
(1)cron )
cron是一个用于在系统上运行命令行作业的linux系统的调度程序。您通过ssh登录到您的服务器,键入 crontab -e
,并添加如下行:
4 5 * * * php /path/to/my/script.php
这将在每天上午5:04运行脚本。
<?php
// /路径/到/ my / script.php
//做某事
一些托管服务允许用GUI进入cron作业。还有外部cron服务,这将在特定时间为您调用URL。
(2)守护程序
这是最先进的选项,也是最不可靠的:运行包含无限循环的命令行脚本。脚本然后定期检查状态并对其进行响应。因为它可能在几个月的运行后崩溃,你必须有一个第二个脚本重新启动,以防万一。这是很多的工作,但它是最灵活的方法。
<?php
while(1){
// fetch $ last_exec_timestamp from database
if($ last_exec_timestamp< time()+ 86400){
//将last_exec_timestamp设置为数据库
//做某事
}
sleep(5);
}
3。用户驱动
如果您的站点上流量过多,您可以简单地将该作业包含在页面页脚中,当没有更多输出确保这段代码很快,或者一个不幸的用户会等待它。
<?php
//从数据库获取$ last_exec_timestamp
if($ last_exec_timestamp< time()+ 86400){
//将last_exec_timestamp设置为数据库
// do东西
}
还有更多的花哨的用户驱动方法,我避风港在另一个堆栈溢出问题中进行个人测试。
I am developing a web app which requires events. The user creates an event at a specific time, and I need a script to be executed and activate the event at the exact time, so the event can start. Any ideas on how I can do this? I googled and searched through stack overflow, but all I encountered was multiple execution at specific time. What I need is execution at automatically set time. Hope you help me, I'm desperate.
You have 3 options. My recommendation: Use cron if you can, user driven if you have to, and daemon as a last resort.
(1) cron (as mentioned in comments)
cron is a scheduler for linux systems that will run a command line job on your system. You log into your server over ssh, type crontab -e
, and add a line like this:
4 5 * * * php /path/to/my/script.php
This would run the script at 5:04 a.m. every day.
<?php
// /path/to/my/script.php
// Do something
Some hosting services allow entering cron jobs with a GUI. There are also external cron services, that will call a URL for you at specific times.
(2) daemon
This is the most advanced option and also the least reliable: You run a command line script that contains an infinite loop. The script then periodically checks state and responds to it. Because it is likely to crash after months of running, you have to have a second script to restart it in case it does. This is a lot of work, but it is the most flexible approach.
<?php
while (1) {
// fetch $last_exec_timestamp from database
if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}
sleep(5);
}
3. user driven
If you have a decent amount of traffic on your site, you can simply include a the job this in your page footer, when there is no more output. Make sure this code is fast, or an unlucky user will be waiting for it.
<?php
// fetch $last_exec_timestamp from database
if ($last_exec_timestamp < time() + 86400) {
// set last_exec_timestamp to now in database
// do something
}
There are also to more fancy approaches of "user driven" that I haven't personally tested in another stack overflow question.
这篇关于在特定时间自动安排php脚本执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!