如何使用 cron 作业发送 HTML GET 请求?
我想设置一个 cron 作业,将 http 请求发送到 url。我该怎么做?我以前从未设置过 cronjob。
I would like to set up a cron job which sends an http request to a url. How would I do this? I have never set up a cronjob before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
cron 作业只是一个按预先设定的时间间隔在后台执行的任务。
您几乎可以用任何语言编写该工作的实际代码 - 甚至可以是简单的 php 脚本或 bash 脚本。
PHP 示例:
接下来,安排 cron 作业:
...上述脚本将在后台每 10 分钟运行一次。
这是一个很好的 crontab 教程:http://net.tutsplus。 com/tutorials/other/scheduling-tasks-with-cron-jobs/
您还可以使用 cURL 来执行此操作,具体取决于您要使用的请求方法:
A cron job is just a task that get's executed in the background at regular pre-set intervals.
You can pretty much write the actual code for the job in any language - it could even be a simple php script or a bash script.
PHP Example:
Next, schedule the cron job:
... the above script will run every 10 minutes in the background.
Here's a good crontab tutorial: http://net.tutsplus.com/tutorials/other/scheduling-tasks-with-cron-jobs/
You can also use cURL to do this depending on what request method you want to use:
您很可能希望以普通(即非特权)用户身份执行此操作,因此假设您以非 root 用户身份登录:
添加此条目以每 5 分钟执行一次 http 请求:
最好您不希望
script.sh
在没有错误时给出任何输出,因此> /dev/null
。否则 cron 将每 5 分钟通过电子邮件将输出发送给(很可能是 root)。这应该足以让您开始。此时,如果您花一些时间来了解一些有关 cron 的知识,那就太好了。阅读适当的 cron 手册页会很好:
从驱动 cron 作业的格式开始:
然后阅读实际的守护进程:
一般建议:养成阅读遇到的任何新 unix 工具的手册页的习惯,不要立即复制和粘贴命令行片段,而是花一些时间阅读开关的作用。
Most probably you want do do this as a normal (i.e. non-privileged) user, so assuming that you are logged in as non-root user:
Add this entry to do the http request once every 5 minutes:
Preferably you don't want
script.sh
to give any output when there is no error, hence the> /dev/null
. Otherwise cron will email the output to (most likely root) every 5 minutes.That should be enough to get you started. At this point it's good if you invest some time to learn a bit about cron. You'll do well bu reading the appropriate man page for cron:
Start with the format for driving the cron jobs:
Then with the actual daemon:
General advice: make it a habit of reading the man page of any new unix tools that you encounter, instead of immediately copying and pasting command line snippets, spend some time reading what the switches do.
为什么不能使用wget。这是另一个教程。
Why can't you use wget. Here is another tutorial.
如果您使用linux
添加curl命令
并每天10:15 ,您将发送html GET请求到 http://test.com参数 "some" 和值 "crontab"
if you use linux
and add curl command
every day 10:15 you will send html GET request to http://test.com with param "some" and value "crontab"