要使用sleep()或cron作业 [英] To use sleep() or cron job

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

问题描述

我有这个邮件脚本,我必须运行几次。
要启动脚本,我将使用cron,但脚本必须再运行2或3次(相隔一小时)。

I have this mail script I have to run a few times. To start the script I will use cron, but the script has to run 2 or 3 more times (with an hour apart).

这是最好的方法是什么?
要使用sleep命令一个小时,或在脚本结束时,放置一些代码,以便脚本将创建一个新的cron作业,在一个小时后自己运行它?

What's the best way to do this? To use the sleep command for an hour, or at the end of the script, place some code, so that the script will create a new cron job to run it self after an hour?

感谢

推荐答案

除非在内存中运行脚本,更好地使用cron根据需要每小时调用它。

Unless there's some cost savings in keeping the script running in memory, you're better off using cron to invoke it every hour, as needed.

0 0-2 * * * /usr/local/bin/mail-script.php

您可以使用 - 语法或逗号语法选择多个小时:

You can choose multiple hours using the - syntax, or the comma syntax:

0 0,1,2,3 * * * /usr/local/bin/mail-script.php

如果需要保持某种形式的状态,请使用临时文件保存保存状态。

If it needs to maintain some form of state, use a temporary file to keep saved state.

/ p>

Do:

> man 5 crontab

查看您的* nix是否处理上述情况。

To see if your *nix handles the above cases.

最后,除非你知道脚本只需要运行 2-3次,你最好是关于是否运行或不运行的逻辑PHP脚本本身,然后每小时运行一次。

Finally, unless you know the script has to run only 2-3 times, you're better off putting the logic about whether to "run or not to run" in the PHP script itself, and then just run it every hour.

这篇关于要使用sleep()或cron作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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