PHP:运行预定作业(cron 作业) [英] PHP: running scheduled jobs (cron jobs)

查看:36
本文介绍了PHP:运行预定作业(cron 作业)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 webhotel 上有一个站点,我想在其上运行一些计划任务.您会推荐哪些方法来实现这一目标?

I have a site on my webhotel I would like to run some scheduled tasks on. What methods of achieving this would you recommend?

到目前为止,我想到的是在每个页面的顶部包含一个脚本,然后让该脚本检查是否该运行该作业.

What I’ve thought out so far is having a script included in the top of every page and then let this script check whether it’s time to run this job or not.

这只是我所想的一个简单例子:

This is just a quick example of what I was thinking about:

if ($alreadyDone == 0 && time() > $timeToRunMaintainance) {
   runTask();
   $timeToRunMaintainance = time() + $interval;
} 

还有什么我应该考虑的吗?还有比这更好的方法吗?

Anything else I should take into consideration or is there a better method than this?

推荐答案

这就是 cronjobs 的用途.man crontab 假设您正在运行 Linux 服务器.如果您没有 shell 访问权限或无法设置 cronjobs,可以使用免费服务在外部服务器上设置 cronjobs 并 ping 您的 URL 之一.

That's what cronjobs are made for. man crontab assuming you are running a linux server. If you don't have shell access or no way to setup cronjobs, there are free services that setup cronjobs on external servers and ping one of your URLs.

这篇关于PHP:运行预定作业(cron 作业)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆