在预定时间运行 PHP 文件?

发布于 2024-12-13 18:11:04 字数 247 浏览 1 评论 0原文

我有一个 php 文件,它从外部 API 中提取一些数据,我想安排它每隔几个小时(或每隔几天)执行一次。一些谷歌搜索让我找到了“计划任务”,但似乎我需要运行自己的服务器才能做到这一点?

到目前为止,我所做的所有PHP和MySQL都是非常简单的填表,所以我有点失落。我是否需要将计算机变成服务器才能执行此操作,或者我应该寻找允许运行脚本的主机?我不太确定我在寻找什么。

附带问题:我如何才能阻止其他人运行 PHP 脚本(因此进行大量 API 调用)?

I have a php file which pulls some data from external API's, and I want to schedule it to do so every few hours (or every few days). Some googleing led me to "scheduled tasks", but it seems I need to be running my own server to do it?

So far, all the PHP and MySQL I've done have been very simple form-filling, so I'm a little lost. Do I need to turn a computer into a server to do this, or should I look into hosts that allow you to run scripts? I'm not exactly sure what I'm looking for.

Side-question: how would I be able to prevent someone else from running the PHP script (therefor making tons of API calls)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女猫性小仙女 2024-12-20 18:11:04

您现在运行该脚本的情况如何? Windows 还是 Linux? Linux 上的 cron 是轻而易举的事:在支持 PHP 的服务器上,只需将 PHP 脚本放到某处,编辑 crontab 就可以了!

前任。每 2 小时

0 */2 * * * /usr/local/bin/php /path/to/script.php

编辑回复:Mac

launchd 显然是运行计划任务的首选方法,但我知道 OS X 具有 cron 功能,并且是 UNIX 的衍生版本。

How are you running the script now? Windows or Linux? Linux is a no-brainer with cron: on a PHP-enabled server simply drop the PHP script somewhere, edit the crontab and away you go!

Ex. for every 2 hours

0 */2 * * * /usr/local/bin/php /path/to/script.php

Edit Re: Mac

launchd is apparently the preferred method to run scheduled tasks but I understand that OS X has cron capabilities as well being a UNIX derivative.

七堇年 2024-12-20 18:11:04

如果您有一个相当繁忙的 Web 服务器,您只需检查自上次运行脚本以来已经过去了多长时间。如果超过两个小时,就运行它。

只需确保更新时间并自动运行脚本,这样您就不会启动脚本的多个副本。您可以使用包含您在检查和更新脚本时锁定的上次运行脚本的文件来执行此操作。

If you have a reasonably busy web server, you can simply check every time how long it has been since the last time you ran the script. If more than two hours, run it.

Just make sure to update the time and run the script atomically so you don't launch several copies of the script. You can do this with a file that contains the last time the script was run that you lock while you check and update it.

天荒地未老 2024-12-20 18:11:04

cronjobs 就是为此而生的...您可以在 cpanel 中检查 Cron Jobs..
我假设您的网站是在 Linu 环境中启动的

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

http://man.cx/cron

您可以找到有关后台进程的更多说明

http://www.fijiwebdesign.com/blog/create-a-background-process-on-the-server-with-php.html

cronjobs are made for it... You can check the Cron Jobs in cpanel..
I am assuming your website is launched in Linu environment

http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

http://man.cx/cron

You can find much more exlaination about the Background Process

http://www.fijiwebdesign.com/blog/create-a-background-process-on-the-server-with-php.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文