有其他 Cron 作业替代方案吗?
我的服务器上的 Cron 作业已关闭,服务器管理员不接受打开它。因为,cron 作业会减慢服务器速度等。所以,我需要一个替代方案。
我必须每 2 分钟运行一个 php 文件 (cron.php)。
那么,我该怎么做呢?
Cron Jobs are closed on my server and server admin doesn't accept open it. Because , cron jobs slowing server etc. So, i need an alternative.
I have to run a php file (cron.php) every 2 minutes.
So, how can i do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管这个问题是不久前发布的,但我也遇到了同样的问题,但找到了解决方案(基于 Kissaki 的回答,谢谢!)并认为我会将其发布在这里,供仍在寻找可能解决方案的任何人使用。
先决条件:
代码 (python):
Even though the question was posted a while ago, I just had the same problem but found a solution (based on Kissaki's answer, thanks!) and thought I'd post it here for anyone still looking for a possible solution.
Prerequisites:
Code (python):
取决于您对盒子的访问权限。
PHP 本身无法很好地独立运行。您可以编写一个脚本,尝试不断增加其执行时间,定期休眠并检查新作业。但这是次优的,因为您必须通过浏览器访问它一次,并且脚本必须确保它只运行一次。
通过 shell 访问,您可以在 shell 上运行 php 脚本,这将阻止它被公共调用并必须通过 web 浏览器运行它。
通过 shell 访问,您还可以运行一个为您提供 (cron) 服务的程序。无论是 Java、Python 还是其他程序。
Cron 作业不应该总是减慢服务器速度。这取决于运行的作业。如果您的工作如此昂贵,您的管理员可能不会接受解决已关闭的 cron 作业并再次减慢服务器速度,并且可能会采取进一步的措施来阻止您解决问题。
Depends on your access on the box.
PHP itself will not be able to run standalone that well. You could do a script which tries to increase it’s execution time constantly, sleeping and checking for new jobs regularly. But that is sub-optimal as you’ll have to access it via browser once, and the script would have to make sure it only runs once.
With shell access you could run the php script on the shell, which would prevent it from being callable from public and having to run it via webbrowser.
With shell access you could also run a program that provides a (cron) service for you. Be it a Java, Python, or other program.
Cron jobs shouldn’t slow the server always. That depends on the job that is run. If it’s your jobs that are so expensive your admin will probably not be okay with working around the closed cron jobs and slowing the server again anyway and may take further action to prevent you from working around.
Linux 有多种 Cron 替代方案。
就像,
Anacron - Anacron 是一个周期性命令调度程序,就像 cron 一样。唯一的区别是它不需要您的计算机始终运行。
fcron - Fcron 是 cron 和 anacron 中最好的。它不需要您的计算机 24×7 运行,并且可以每小时或每分钟处理任务。
Hcron
Jobber
等等
There are several Cron alternatives for Linux.
Like,
Anacron - Anacron is a periodic command scheduler just like cron. The only difference is that it does not need your computer to be always running.
fcron - Fcron is the best of both cron and anacron. It doesn’t require your computer to be running 24×7, and it can work with tasks on an hourly or minute basis.
Hcron
Jobber
etc.
只是对答案的补充。罕见的情况。
如果您的应用程序经常进行大量数据库操作,那么您可以维护一个单独的表,其中的一列将作为何时运行脚本的度量。示例如下
表 CRON_RUN
现在,在您的应用程序中,每次发生这些频繁的数据库操作时都可以执行检查,并检查 last_run 日期和当前日期是否有 x 差异。 X 是您想要运行脚本的时间差。你的情况是 2 分钟。此外,如果差异大于或等于 2 分钟,则将运行更新语句以使用当前日期时间更新 last_run 列,下一条语句将是您的 cron.php 脚本。
Just an addition to the Answers. Rare case scenario.
If your application is having frequent amount of database operations then you can maintain a separate table where the the one column will work as a measure when to run the script. Example as below
Table CRON_RUN
Now in your application a check can be performed every time when those frequent db operations occurs and check if the last_run date and the current date is having x difference . X is difference in time you want to run the script. 2 minutes in your case. Also if the difference is more then or equal to 2 minutes then the update statement will run to update the last_run column with the current date time and the next statement will be you cron.php script .
最佳解决方案:- 使用序列化文件来保存状态或数据库。在每个请求时检查此文件并在需要时运行脚本。如果您需要更广泛地管理这些作业并运行 shell 命令,并且您有安装服务的权限。使用 SartajPHP NativeApp 作为服务。您还可以通过浏览器控制您的作业并查看实时错误并修复它们。
Best Solution:- Use serialise file to save status or DB. Check this file on every request and run script when need. If you need to manage these jobs more widely and run shell commands and you have permission to install services. Use SartajPHP NativeApp as service. You can also control your jobs via browser and see live errors and fix them.