每 24 小时运行一次 php 任务
我有一些函数使用curl 从几个站点中提取信息并将其插入到我的数据库中。 我只是想知道每 24 小时执行此任务的最佳方法是什么?
我现在正在运行 Windows,但一旦我上线,可能会切换到 Linux(如果这有影响的话)。 我现在在 symfomy 框架内工作。
我听说 cronjobs 可以做到这一点......但看看该网站,它似乎可以远程工作,我宁愿把东西放在家里......我可以在我的计算机上“运行服务”吗? 无论这意味着什么;)(听说过)
感谢您的帮助, 安德鲁
I have some functions that use curl to pull information off a couple of sites and insert them into my database. I was just wondering what is the best way to go about executing this task every 24 hours?
I am running off windows now, but will probably switch to linux once I am live (if that makes a difference). I am working inside symfomy framework now.
I hear cronjobs can do this this...but looking at the site it seems to work remotely and I would rather just keep things in house...Can i just "run a service" on my computer? whatever that means ;) (have heard it used)
thanks for any help,
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这正是 Cron (linux) 或计划任务 (windows) 的用途。
您可以在应用程序服务器上运行它们,以将所有内容保存在一处。
例如,我在我的家庭服务器上运行一个 cron 来每天备份其 MySQL 数据库。 这一过程仅涉及一个系统。
This is exactly what Cron (linux) or Scheduled Tasks (windows) are for.
You can run them on your application server to keep everything in one place.
For example, I have a cron running on my home server to backup its MySQL databases every day. Only one system is involved in this process.
将
0 0 * * * php /path/to/your/cronjob.php
添加到您的 crontab 应该可以完成此任务。Adding
0 0 * * * php /path/to/your/cronjob.php
to your crontab should accomplish this.可以在cron中设置计划任务(或者在windows中设置计划任务)。 最简单的方法是创建一个 shell 脚本(Windows 中的批处理脚本),从命令行执行 php 脚本(因此您不必使用 www 服务器资源)。 当然,您在目标机器上执行脚本。
You can set a scheduled task in cron (or a scheduled task in windows). The easiest way is to create a shell script (batch script in windows) that executes php script from command line (thanks to this you don't have to use www server resources). Of course you execute the script on the target machine.
如果您认为 cron 或 windows 调度程序不合适,有时我发现编写一个快速的 Java 应用程序来完成同样的事情很方便:
您可以使用调用
System.getRuntime().exec(" cmd 行内容在这里");
。 然后,您可以在 TimerTask 中扭曲该操作。 最后,您通过添加 TimerTasks 并指定时间和频率等来启动一个 Timer 对象...这显然比上面提到的示例更复杂,但我喜欢它,因为您可以抛出一些智能错误当出现问题时处理并向自己发送电子邮件警报等。
可能有点矫枉过正,但如果您曾经有过几次这样的操作需要处理,那么可能值得一看。
理发师
If for whatever you decide that cron or windows scheduler is not appropriate, I've sometimes found it handy to write a quick Java app that does the same thing:
You can use the calls
System.getRuntime().exec("cmd line stuff here");
. You can then warp that operation in aTimerTask
. Finally, you fire up aTimer
object by adding TimerTasks and specifying the times and frequency etc...This is clearly more complicated than the above mentioned examples however I like it because you can throw in some intelligent error handling and send yourself email alerts or the like when something screws up.
Probably overkill but possibly worth looking at if you ever have several such operations to deal with.
sweeney