WordPress 的 cron 任务
我想使用 cron 作业向 Wordpress 添加一项密集任务(假设执行时间为 5 分钟)。
我一直在使用此代码在 Wordpress 系统内添加新的 cron 任务。
wp_schedule_event(time(), "interval-name", "hook-name");
我在网上的某个地方读到,当有请求击中 Wordpress 时(无论是在公共站点还是在管理员中),将执行 cron 任务。有人能承认这是真的吗?
如果是这样的话,那么我不应该将密集型任务放入 cron 任务中,因为这会让用户在任务完成后等待很长时间。我现在该怎么办?
有人经历过这种情况吗?有什么建议吗? 我想创建一个由 crontab 执行的新页面(例如 http://example.com/wp-content/plugins/plugin-example/intense-task.php)
I would like to add an intensive task (lets say 5 minutes execution time) into Wordpress using cron job.
I been using this code to add new cron task inside the Wordpress system.
wp_schedule_event(time(), "interval-name", "hook-name");
I read somewhere in the net that cron task will be executed when there is request hit the Wordpress (either in the public site or the admin). Can anybody acknowledge that is true?
If that the case then I should not put my intensive task into cron task, because it will make user wait for long time after the task finished. What should I do now?
Anybody experienced this situation? Any suggestion?
I think to create a new page to be executed by crontab (for example http://example.com/wp-content/plugins/plugin-example/intensive-task.php)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
wordpress 文档表示,当有人访问您的网站时它将运行,所以是的,您是正确的。只有一个用户的页面加载速度较慢,因此是否要避免这种情况取决于您。
如果您从常规的骗局工作中运行它,则无需将其作为您网站上的页面;特别是如果这是一项密集型工作,正如您所说,那么这很容易被利用,给您的服务器带来很大的负载。您可以轻松地从命令行运行php来安全地执行您的工作,而不会导致页面上任何缓慢的加载时间。
The wordpress documentation says that it will be run when someone visits your site, so yes, you're correct. It will only be one user that gets a slow page load, so it's up to you if you want to avoid that.
If you are running it from a regular con job, there's no need to make it a page on your site though; especially if it's an intensive job, as you say, then this could easily be exploited place a large load on your server. You can easily run php from the command line to execute your job safely and without causing any slow load times on your page.
如果你使用常规的 cronjob 就不会出现这种情况
但我怀疑 wp 会按照你所说的做,因为这将使其能够在具有不同设置的不同主机上工作,只要它们的 php 和 mysql 独立于真正的 cronjobs 运行,而真正的 cronjobs 必须由网络主机单独安装
If you would use regular cronjob that wouldn't be the case
but i suspect that wp does what you said, since that would make it versatile working in different hosts with different setups as long as they have php and mysql running independent from real cronjobs which must be installed by the web host separately