如何让我的 PHP 脚本每天在特定时间运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4558601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make my PHP script run at a certain time everyday?
提问by Strawberry
I have a PHP script that checks my database's code for validity. How can I have this run everyday at a certain time so then I can just have it run on autopilot.
我有一个 PHP 脚本来检查我的数据库代码的有效性。我怎么能每天在某个时间运行它,这样我就可以让它在自动驾驶仪上运行。
回答by zsalzbank
What you want is called a cron job and is already covered here:
您想要的称为 cron 作业,此处已涵盖:
回答by VSB
If you are using cPanelon your host, you can go to Advanced --> cron jobs. Then inside there you can set a cron job command like this:
如果您在主机上使用cPanel,您可以转到高级 --> cron 作业。然后在里面你可以设置一个像这样的 cron 作业命令:
wget http://yoursite.com/yourfile.php
which http://yoursite.com/yourfile.php
should point to your desired function.
You can also set repetition time interval over there.
这http://yoursite.com/yourfile.php
应该指向您想要的功能。您还可以在那里设置重复时间间隔。
This way yourfile.php
is called in specified intervals.
这种方式yourfile.php
在指定的时间间隔内被调用。
回答by Alfred
CronJobs feature are often disabled on most free or cheap Linux hosting. If you could use cron you could use that.
大多数免费或廉价的 Linux 主机通常禁用 CronJobs 功能。如果您可以使用 cron,您可以使用它。
Alternatives:
备择方案:
- Use services like : http://www.setcronjob.com/(many exist, just search on google) , to schedule your task for free(1x per hour).
- If you need a little bit more juice you should look into google app engine cron. In the cron you just do a simple asynchronous requestto call your page.
- 使用以下服务:http: //www.setcronjob.com/(许多存在,只需在谷歌上搜索),免费安排您的任务(每小时 1 次)。
- 如果您需要更多果汁,您应该查看谷歌应用引擎cron。在 cron 中,您只需执行一个简单的异步请求即可调用您的页面。
回答by moinudin
回答by Vishal P Gothi
May it will helps you.
愿它会帮助你。
You can schedule a task in Windows - control panel->administrative tools is where you find task scheduler.
您可以在 Windows 中安排任务 - 控制面板-> 管理工具是您找到任务计划程序的地方。
run a php automatically after every hour
Thanks
谢谢
回答by user2267379
You can easily use your database to save the last execution of the script.
您可以轻松地使用您的数据库来保存脚本的最后一次执行。
Each execution of a certain page you run a script that does the following :
每次执行某个页面,您都会运行一个执行以下操作的脚本:
Check if more than 8 hours (example)
Checks in the database if the script has already been executed
检查是否超过 8 小时(示例)
检查数据库中的脚本是否已经被执行
If executed: does nothing.
如果执行:什么都不做。
If not executed: starts and updates the date last execution in database.
如果未执行:启动并更新数据库中上次执行的日期。
回答by Oliver A.
If you have your own server you can run a cronjob(unix/linux) or a scheduled task(windows).
如果您有自己的服务器,则可以运行 cronjob(unix/linux)或计划任务(windows)。
If your app/site is hosted on a shared host that does not offer cronjobs you an use a sevice like http://www.onlinecronjobs.com/
如果您的应用程序/站点托管在不提供 cronjobs 的共享主机上,您可以使用像http://www.onlinecronjobs.com/这样的服务
I have not tested this one but If you google a bit(fee conjobs) you will find plenty of those. They will call a certain a url accoding to a fixed schedule. Most of those sevices have a fixed set of ip addeses so you can pevent the scipt from being called by anyone else.
我还没有测试过这个,但是如果你用谷歌搜索一下(费用合并),你会发现很多。他们会根据固定的时间表调用某个网址。大多数这些服务都有一组固定的 ip 地址,因此您可以防止其他人调用 scipt。