PHP:运行预定作业(cron 作业)

2022-01-03 00:00:00 cron scheduling scheduled-tasks php

我的 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.

相关文章