在特定时间自动安排php脚本执行 [英] automatically schedule a php script execution at specific time

查看:474
本文介绍了在特定时间自动安排php脚本执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要事件的网络应用程序。用户在特定时间创建一个事件,我需要一个脚本来执行,并在确切的时间激活事件,所以事件可以开始。
有什么想法可以做到这一点?我通过堆栈溢出来搜索并搜索,但是我遇到的一切都是在特定时间多次执行。我需要的是在自动设置的时间执行。
希望你帮我,我很绝望。

解决方案

你有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆