简体   繁体   English

根据它的运行时间运行func()

[英]run func() based on what time it is

i wrote some code that monitors a directory DIR with inotify() and when a file gets moved in DIR i get a .txt output of that file(its an nfcapd file with flows of my network interface). 我写了一些使用inotify()监视目录DIR的代码,当文件在DIR中移动时,我得到该文件的.txt输出(它是一个带有我的网络接口流的nfcapd文件)。 This happens every 5 minutes. 每5分钟发生一次。

After that, i used Snort's DPX starter kit, with which you can extend Snort by writing your own preprocessor. 之后,我使用了Snort的DPX入门套件,您可以通过编写自己的预处理器来扩展Snort。 This preprocessor,like all the others, is just a function that's executed every time a new packet is available. 与所有其他预处理器一样,此预处理器只是每次有新数据包时执行的函数。 My problem is that i want, when a new file gets exported from my previous code(so every 5 minutes), to read that file inside the preprocessor's function. 我的问题是,我希望,当一个新文件从我之前的代码中导出时(每5分钟一次),在预处理器的函数中读取该文件。

So, is there any way of getting the time and executing only if it's the desired time? 那么,有没有什么方法可以获得时间并且只有在需要的时间才能执行?

if (time is 15:36){
    func(output.txt);}

i'm writing in c. 我在写c。

Thanks 谢谢

You can do something like the following: 您可以执行以下操作:

#include <time.h>
...

time_t t = time(NULL); //obtain current time in seconds
struct tm broken_time;
localtime_r(&t, &broken_time); // split time into fields

if(broken_time.tm_hour == 15 && broken_time.tm_min == 36) { //perform the check
    func(output.txt);
}

Since you're using inotify , I'm assuming your environment supports POSIX signals. 由于您使用的是inotify ,我假设您的环境支持POSIX信号。

You can use alarm() to raise a signal after a predetermined amount of time has passed and have the appropriate signal handler do whatever work you need to do. 在预定的时间过去之后,您可以使用alarm()来发出信号,并让适当的信号处理程序完成您需要做的任何工作。 It would avoid what I think is going to end up being a very ugly infinite loop in your code. 它会避免我认为最终会成为代码中非常难看的无限循环。

So, in your case, the function handling SIGALRM would not need to worry what time it was, it would know that a predetermined amount of time has passed by the fact that it was entered. 因此,在您的情况下,处理SIGALRM的函数不需要担心它是什么时间,它会知道输入它的事实已经过了预定的时间量。 However, you'll need to provide some context that function can access to know what to do, kind of hard to suggest how without seeing your code. 但是,你需要提供一些背景下,函数可以访问到知道做什么 ,有种难以建议如何没有看到你的代码。

I'm not entirely sure you're going down the right path with this, but using alarm() would probably be the sanest approach given what you described. 我并不完全确定你是否会沿着正确的道路走下去,但是根据你描述的内容,使用alarm()可能是最安全的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM