使用sleep()或cron作业

时间:2022-09-15 16:46:30

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).

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

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?

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

Thanks

4 个解决方案

#1


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.

除非在保持脚本在内存中运行方面节省一些成本,否则最好根据需要每小时使用cron调用它。

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.

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

Do:

> man 5 crontab

To see if your *nix handles the above cases.

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

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.

最后,除非你知道脚本只需运行2-3次,否则最好在PHP脚本本身中设置是否“运行或不运行”的逻辑,然后每小时运行一次。

#2


One advantage of using sleep() is that it could be more portable. For example, on many systems I work with, users are not allowed to have their own cron jobs - so writing your program to take care of its own timer-ness might be an advantage.

使用sleep()的一个优点是它可以更便携。例如,在我使用的许多系统上,不允许用户拥有自己的cron作业 - 因此编写程序来处理自己的计时器可能是一个优势。

An alternative to sleep() might be using SIGALRM (so your script catches an interrupt and executes code at a certain interval - when that interrupt is thrown.)

sleep()的替代方法可能是使用SIGALRM(因此您的脚本捕获中断并以特定间隔执行代码 - 当抛出该中断时。)

I mean, I'd recommend using cron - but here are some alternatives!

我的意思是,我建议使用cron - 但这里有一些替代品!

#3


I'm not sure either approach (sleeping for an hour, or creating cron jobs from php) is ideal, how about a cron job that runs every hour anyway, then your php script checks whether it should run?

我不确定任何一种方法(睡眠一小时,或者从php创建cron作业)都是理想的,无论如何每小时运行一个cron作业,然后你的php脚本检查它是否应该运行?

#4


Why not just set the cron criteria so it fires those specific times? Cron is pretty flexible in that aspect.

为什么不设置cron标准,以便触发那些特定的时间? Cron在这方面非常灵活。

Update your question with when you want it to fire off and I can give an example.

更新你的问题时,你想要它发射,我可以举一个例子。

#1


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.

除非在保持脚本在内存中运行方面节省一些成本,否则最好根据需要每小时使用cron调用它。

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.

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

Do:

> man 5 crontab

To see if your *nix handles the above cases.

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

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.

最后,除非你知道脚本只需运行2-3次,否则最好在PHP脚本本身中设置是否“运行或不运行”的逻辑,然后每小时运行一次。

#2


One advantage of using sleep() is that it could be more portable. For example, on many systems I work with, users are not allowed to have their own cron jobs - so writing your program to take care of its own timer-ness might be an advantage.

使用sleep()的一个优点是它可以更便携。例如,在我使用的许多系统上,不允许用户拥有自己的cron作业 - 因此编写程序来处理自己的计时器可能是一个优势。

An alternative to sleep() might be using SIGALRM (so your script catches an interrupt and executes code at a certain interval - when that interrupt is thrown.)

sleep()的替代方法可能是使用SIGALRM(因此您的脚本捕获中断并以特定间隔执行代码 - 当抛出该中断时。)

I mean, I'd recommend using cron - but here are some alternatives!

我的意思是,我建议使用cron - 但这里有一些替代品!

#3


I'm not sure either approach (sleeping for an hour, or creating cron jobs from php) is ideal, how about a cron job that runs every hour anyway, then your php script checks whether it should run?

我不确定任何一种方法(睡眠一小时,或者从php创建cron作业)都是理想的,无论如何每小时运行一个cron作业,然后你的php脚本检查它是否应该运行?

#4


Why not just set the cron criteria so it fires those specific times? Cron is pretty flexible in that aspect.

为什么不设置cron标准,以便触发那些特定的时间? Cron在这方面非常灵活。

Update your question with when you want it to fire off and I can give an example.

更新你的问题时,你想要它发射,我可以举一个例子。