每24小时运行一次php任务

时间:2022-06-25 16:58:39

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?

我有一些函数使用curl从几个站点提取信息并将它们插入我的数据库。我只是想知道每24小时执行此任务的最佳方法是什么?

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.

我现在正在运行Windows,但是一旦我活着就可能切换到linux(如果这有所不同)。我现在在symfomy框架内工作。

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)

我听说cronjobs可以做到这一点......但是看看网站似乎远程工作,我宁愿把东西留在家里......我可以在我的电脑上“运行服务”吗?无论那意味着什么;)(听说过它)

thanks for any help, Andrew

谢谢你的帮助,安德鲁

4 个解决方案

#1


This is exactly what Cron (linux) or Scheduled Tasks (windows) are for.

这正是Cron(linux)或Scheduled Tasks(windows)的用途。

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.

例如,我在家用服务器上运行了一个cron来每天备份其MySQL数据库。此过程只涉及一个系统。

#2


Adding 0 0 * * * php /path/to/your/cronjob.php to your crontab should accomplish this.

将0 0 * * * php /path/to/your/cronjob.php添加到您的crontab应该可以实现此目的。

#3


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中设置计划任务)。最简单的方法是创建一个shell脚本(windows中的批处理脚本),从命令行执行php脚本(由于这个原因,你不必使用www服务器资源)。当然,您在目标计算机上执行脚本。

#4


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:

如果您认为cron或Windows调度程序不合适,我有时会发现编写一个快速的Java应用程序可以做同样的事情:

You can use the calls System.getRuntime().exec("cmd line stuff here");. You can then warp that operation in a TimerTask. Finally, you fire up a Timer object by adding TimerTasks and specifying the times and frequency etc...

您可以使用调用System.getRuntime()。exec(“cmd line stuff here”);.然后,您可以在TimerTask中扭曲该操作。最后,通过添加TimerTasks并指定时间和频率等来启动Timer对象...

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

#1


This is exactly what Cron (linux) or Scheduled Tasks (windows) are for.

这正是Cron(linux)或Scheduled Tasks(windows)的用途。

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.

例如,我在家用服务器上运行了一个cron来每天备份其MySQL数据库。此过程只涉及一个系统。

#2


Adding 0 0 * * * php /path/to/your/cronjob.php to your crontab should accomplish this.

将0 0 * * * php /path/to/your/cronjob.php添加到您的crontab应该可以实现此目的。

#3


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中设置计划任务)。最简单的方法是创建一个shell脚本(windows中的批处理脚本),从命令行执行php脚本(由于这个原因,你不必使用www服务器资源)。当然,您在目标计算机上执行脚本。

#4


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:

如果您认为cron或Windows调度程序不合适,我有时会发现编写一个快速的Java应用程序可以做同样的事情:

You can use the calls System.getRuntime().exec("cmd line stuff here");. You can then warp that operation in a TimerTask. Finally, you fire up a Timer object by adding TimerTasks and specifying the times and frequency etc...

您可以使用调用System.getRuntime()。exec(“cmd line stuff here”);.然后,您可以在TimerTask中扭曲该操作。最后,通过添加TimerTasks并指定时间和频率等来启动Timer对象...

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