有什么方法可以使用 cron 在托管 Web 服务器上自动运行 PHP 脚本吗?
我有一个每天发送短信警报的脚本。我希望它通过从数据库获取消息来自动发送短信。我可以在没有 cron 的情况下完成它吗?还有其他解决办法吗。请帮忙
I had a script which sends sms alerts everyday. I want it to send automatically send sms by fetching message from database. Can I do it without cron. Is there any other solution. Plz help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
拥有外部 AppEngine cron 作业调度程序:使用 google AppEngine,它免费。然后,外部“任务”可以“回调”您的 PHP 脚本。
尽管除了您无法访问它之外,我还是看不出在您的计算机上使用本地
cron
有什么问题。Have an outside AppEngine cron job sheduler: Use google AppEngine, it's free. The outside "task" can then "callback" a PHP script of yours.
I fail to see what's wrong with using the local
cron
on your machine though aside from it being blocked for you to access.您可以使用 webcron 服务来触发脚本。
You can use a webcron service to trigger your script.
简而言之,如果不使用某种调度程序或 cron,就无法运行 php 脚本...
In simple words, you can't run php scripts without using some sort of scheduler or cron...
您有几个选项:
模拟 cron 行为:在您的 php 代码中,检查页面是否在您希望安排发送的时间或接近的时间加载,如果消息尚未发送,则加载在后台发送消息并将其标记为已发送的脚本。
正如其他人所建议的,在其他地方设置一个 cronjob,它将通过 http 获取 url(例如使用 wget)在您的服务器上执行脚本
You have a couple of options:
Emulate cron behavior: In your php code, check if the page was loaded at, or close to, the time you wish to schedule the sendout for, if the messages havent been sent yet, load the script that sends the messages in the background and flag them as sent.
As others have suggested, setup a cronjob somewhere else that will execute a script on your server by fetching the url via http (by using wget for example)
在其他地方设置一个 cron 任务,让它请求发送警报的脚本。
set up a cron task elsewhere, have it request the script that sends the alerts.
您可以包含您的脚本并让它在每次有人访问您的主页/登录页面等时运行...
在mysql中建立一个名为time的表。
每次访问页面时都会查询数据库。 if (time argument) {在后台运行脚本,更新数据库中的新时间}
您所要做的就是计算出您想要发送短信电子邮件的频率。
前任。
每天1次
数据库行“时间”中有一个日期。可以说日期是 24 小时前。
如果数据库行“时间”小于或等于今天减去一天 1 天,则运行脚本。这将每天运行您的脚本一次。
缺点->有人需要每天至少访问您的网站一次。
您的网站网址是什么?你在干什么?
you can include your script and have it run every time some one visits your homepage/login page etc...
set up a table in mysql called time.
query the database everytime the page is visited. if (time argument) {run the script in the background, update the new time in the DB}
All you have to do is fuigure out how often you want to send out your sms emails.
Ex.
1 time per day
database row "time" has a date in it. the date is lets say 24 hours ago.
if the db row "time" is less than or equal to today minus one day 1 day , then run your script. this will run your script once per day.
The draw back -> someone needs to visit your site atleast once per day.
What is your website URL? What are you doing?
使用第三方 webcron,例如 easycron.com。
Using third party webcron like easycron.com.
如果您无法在服务器上设置 cron 作业,请在您有权访问的客户端(例如您的计算机)上执行此操作,以便它在您需要运行脚本时发出请求。
If you can't set up a cron job on the server, do it on a client you have access to (like your computer) so that it does a request when you need the script to be run.
这不是最佳的,但是在不使用任何类型的调度程序的情况下,您可以做的就是这样。
这样做的缺点是:
It's not optimal, but without using any kind of scheduler what you could do is this.
Down sides of this is that:
我使用 Windows 任务计划程序在我之前无法访问 cron 的服务器上安排一个脚本,效果很好。
I've used windows Task Scheduler to schedule a script on a server I couldn't access cron on before, that worked fine.
这不是最好的答案,因为它仍然需要 cron 某处,但您始终可以设置另一台具有 cron 的计算机(例如您的家庭计算机)来访问您网站上的页面,该页面会触发操作(使用像
curl
或wget
这样的库)。This isn't the best answer as it still requires cron somewhere, but you could always setup another machine that does have cron, like your home computer for instance, to hit a page on your site which triggers the action (using a library like
curl
orwget
).