如何在固定的时间间隔后运行我的程序代码?

发布于 2024-09-19 11:24:14 字数 763 浏览 2 评论 0 原文

我有 这个 Perl 脚本 用于监视 Linux 中的文件夹。

为了不断检查目录的任何更新,我有一个 while 循环,在连续循环之间休眠 5 分钟:

while(1) {

    ...
    sleep 300;
}

Nobody on 我的其他问题建议使用cron进行调度而不是for循环。

与使用 while 结构在我看来很难看>crontab

0 */5 * * * ./myscript > /dev/null 2>&1
  • cron 是正确的选择吗?使用 while 循环结构有什么优点吗?
  • 除了循环和 cron 之外,还有其他更好的方法吗?

另外,我使用的是 2.6.9 内核版本。

I have this Perl script for monitoring a folder in Linux.

To continuously check for any updates to the directory, I have a while loop that sleeps for 5 minutes in-between successive loops :

while(1) {

    ...
    sleep 300;
}

Nobody on my other question suggested using cron for scheduling instead of a for loop.

This while construct, without any break looks ugly to me as compared to submitting a cronjob using crontab :

0 */5 * * * ./myscript > /dev/null 2>&1
  • Is cron the right choice? Are there any advantages of using the while loop construct?
  • Are there any better ways of doing this except the loop and cron?

Also, I'm using a 2.6.9 kernel build.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

殤城〤 2024-09-26 11:24:14

我使用 while 解决方案的唯一原因是,如果我需要我的代码每分钟运行一次以上,或者如果它需要立即响应外部事件,这两种情况似乎都不是这里的情况。

我的想法通常是这样的:cron 已经经过数十年数百万人的测试,因此它至少与我刚刚串在一起的代码一样可靠。

即使在我使用了 while 的情况下,我仍然有一个 cron 作业来在发生故障时重新启动我的脚本。

我的建议是简单地使用cron。这就是它的设计目的。而且,顺便说一句,我很少将输出重定向到 /dev/null,这使得调试变得非常困难。通常我只是重定向到 /tmp 文件系统中的一个文件,以便我可以看到发生了什么。

只要您有自动清理程序,您就可以追加,如果您担心有人看到输出中的内容,您甚至可以写入更私密的位置。

但最重要的是,如果您丢弃输出,则无法分析罕见的故障。如果您认为您的工作没有错误,那么无论如何,扔掉输出,但我很少认为我的脚本没有错误,以防万一。

The only reasons I have ever used the while solution is if either I needed my code to be run more than once a minute or if it needed to respond immediately to an external event, neither of which appear to be the case here.

My thinking is usually along the lines of: cron has been tested by millions and millions of people over decades so it's at least as reliable as the code I've just strung together.

Even in situations where I've used while, I've still had a cron job to restart my script in case of failure.

My advice would be to simply use cron. That's what it's designed for. And, as an aside, I rarely redirect the output to /dev/null, that makes it too hard to debug. Usually I simply redirect to a file in the /tmp file system so that I can see what's going on.

You can append as long as you have an automated clean-up procedure and you can even write to a more private location if you're worried about anyone seeing stuff in the output.

The bottom line, though, is that a rare failure can't be analysed if you're throwing away the output. If you consider your job to be bug-free then, by all means, throw the output away but I rarely consider my scripts bug-free, just in case.

喜你已久 2024-09-26 11:24:14
  1. 为什么不让将构建放入目录的构建过程执行通知? (请参阅 SO 3691739 了解其来源!)< /p>

  2. < p>让 cron 运行程序是完全可以接受的 - 并且比带有睡眠的永久循环简单,尽管不是很多。

  3. 针对 cron 解决方案,由于该过程是一个简单的一次性操作,因此您无法判断自上次运行以来发生了什么变化 - 没有状态。 (或者,更准确地说,如果您通过文件提供状态,可能会比运行在内部保留其状态的单个脚本复杂得多。)

  4. 此外,停止通知服务不太明显。如果有一个进程闲置,您可以将其杀死并停止通知。如果通知是由 cron 运行的,那么您必须知道它们已经用完了 crontab,知道它是谁的 crontab,并编辑该条目以停止它。

  5. 您还应该考虑说服您的公司升级到可以使用 inotify 机制的 Linux 版本。

  1. Why don't you make the build process that puts the build into the directory do the notification? (See SO 3691739 for where that comes from!)

  2. Having cron run the program is perfectly acceptable - and simpler than a permanent loop with a sleep, though not by much.

  3. Against a cron solution, since the process is a simple one-shot, you can't tell what has changed since the last time it was run - there is no state. (Or, more accurately, if you provide state - via a file, probably - you are making life much more complex than running a single script that keeps its state internally.)

  4. Also, stopping the notification service is less obvious. If there's a single process hanging around, you kill it and the notifications stop. If the notifications are run by cron, then you have to know that they're run out of a crontab, know whose crontab it is, and edit that entry in order to stop it.

  5. You should also consider persuading your company to upgrade to a version of Linux where the inotify mechanism is available.

天邊彩虹 2024-09-26 11:24:14

如果您选择循环而不是 cron 并希望您的作业定期运行,那么 sleep(300) 往往会漂移。 (考虑脚本其余部分的执行时间)

我建议使用如下结构:

use constant DELAY => 300;

my $next=time();
while (1){
    $next+=DELAY;

    ...;

    sleep ($next-time());
};

If you go for the loop instead of cron and want your job run at regular intervals, sleep(300) tends to drift. (consider the execution time of the rest of your script)

I suggest using a construct like this:

use constant DELAY => 300;

my $next=time();
while (1){
    $next+=DELAY;

    ...;

    sleep ($next-time());
};
妳是的陽光 2024-09-26 11:24:14

另一种选择是“anacron”实用程序。

Yet another alternative is the 'anacron' utility.

做个ˇ局外人 2024-09-26 11:24:14

如果您不想使用cron
这个 http://upstart.ubuntu.com/ 可用于托管进程。
或者您可以使用 watch 哪个更简单。

if you don't want to use cron.
this http://upstart.ubuntu.com/ can be used to babysit processes.
or you can use watch whichever is easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文