linux使用Inotify监控目录或者文件状态变更


基本概念:

Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。


需求:

1.有一个文件采集进程,需要对日志文件进行采集,日志文件可能会被删除,可能会被移动。

2.我们都知道文件一旦被删除或者移动,那么进程使用原有打开的文件fd就无法继续读取文件数据。

3.那么就需要监控文件的创建,移动,删除等状态,以便重新打开文件,所以需要使用Inotify来做这件事。


源码inotfy.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>

#define EVENT_NUM 12
char *event_str[EVENT_NUM] =
{
"IN_ACCESS",
"IN_MODIFY",        //文件修改
"IN_ATTRIB",
"IN_CLOSE_WRITE",
"IN_CLOSE_NOWRITE",
"IN_OPEN",
"IN_MOVED_FROM",    //文件移动from
"IN_MOVED_TO",      //文件移动to
"IN_CREATE",        //文件创建
"IN_DELETE",        //文件删除
"IN_DELETE_SELF",
"IN_MOVE_SELF"
};

int main(int argc, char *argv[])
{
    int fd;
    int wd;
    int len;
    int nread;
    char buf[BUFSIZ];
    struct inotify_event *event;
    int i;

    // 判断输入参数
    if (argc < 2) {
        fprintf(stderr, "%s path\n", argv[0]);
        return -1;
    }

    // 初始化
    fd = inotify_init();
    if (fd < 0) {
        fprintf(stderr, "inotify_init failed\n");
        return -1;
    }

    /* 增加监听事件
     * 监听所有事件:IN_ALL_EVENTS
     * 监听文件是否被创建,删除,移动:IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO
     */
    wd = inotify_add_watch(fd, argv[1], IN_CREATE|IN_DELETE|IN_MOVED_FROM|IN_MOVED_TO);
    if(wd < 0) {
        fprintf(stderr, "inotify_add_watch %s failed\n", argv[1]);
        return -1;
    }

    buf[sizeof(buf) - 1] = 0;
    while( (len = read(fd, buf, sizeof(buf) - 1)) > 0 ) {
        nread = 0;
        while(len> 0) {
            event = (struct inotify_event *)&buf[nread];
            for(i=0; i<EVENT_NUM; i++) {
                if((event->mask >> i) & 1) {
                    if(event->len > 0)
                        fprintf(stdout, "%s --- %s\n", event->name, event_str[i]);
                    else
                        fprintf(stdout, "%s --- %s\n", " ", event_str[i]);
                }
            }
            nread = nread + sizeof(struct inotify_event) + event->len;
            len = len - sizeof(struct inotify_event) - event->len;
        }
    }

    return 0;
}

编译运行:

gcc inotfy.c

// 监控当前目录的文件变化
./a.out ./

测试结果:



小结:

1.可以根据需要对代码进行调整测试

2.参考:http://www.ibm.com/developerworks/cn/linux/l-inotify/

3.参考:http://www.jb51.net/article/37420.htm


End;

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用 epoll 和 inotify 监控文件,需要进行以下步骤: 1. 创建一个 epoll 实例并将其添加到监听文件描述符列表中。 2. 创建一个 inotify 实例并将其添加到 epoll 实例中。 3. 使用 inotify_add_watch 函数添加要监视的文件目录。 4. 启动 epoll 循环,并等待事件发生。 5. 当有事件发生时,使用 epoll_wait 函数获取事件列表。 6. 遍历事件列表,处理每个事件。如果是 inotify 事件,则读取事件并处理它。 7. 如果需要继续监视文件,则重复步骤 4-6。 下面是一个简单的示例代码,用于监视目录中的文件创建或删除事件: ```c #include <stdio.h> #include <stdlib.h> #include <sys/epoll.h> #include <sys/inotify.h> #define MAX_EVENTS 10 #define EVENT_SIZE (sizeof (struct inotify_event)) #define BUF_LEN (MAX_EVENTS * (EVENT_SIZE + 16)) int main(int argc, char const *argv[]) { int fd, wd, epfd, n, i; char buf[BUF_LEN]; struct epoll_event event; struct epoll_event events[MAX_EVENTS]; // create an inotify instance fd = inotify_init(); if (fd < 0) { perror("inotify_init"); exit(EXIT_FAILURE); } // add the inotify instance to epoll epfd = epoll_create(1); if (epfd < 0) { perror("epoll_create"); exit(EXIT_FAILURE); } event.data.fd = fd; event.events = EPOLLIN | EPOLLET; if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) < 0) { perror("epoll_ctl"); exit(EXIT_FAILURE); } // add directory to watch list wd = inotify_add_watch(fd, "/path/to/directory", IN_CREATE | IN_DELETE); if (wd < 0) { perror("inotify_add_watch"); exit(EXIT_FAILURE); } while (1) { // wait for events n = epoll_wait(epfd, events, MAX_EVENTS, -1); if (n < 0) { perror("epoll_wait"); break; } for (i = 0; i < n; i++) { if (events[i].data.fd == fd) { // read inotify events int len = read(fd, buf, BUF_LEN); if (len < 0) { perror("read"); break; } char *p = buf; while (p < buf + len) { struct inotify_event *event = (struct inotify_event *) p; printf("event: %s\n", event->name); p += sizeof(struct inotify_event) + event->len; } } } } // cleanup inotify_rm_watch(fd, wd); close(fd); close(epfd); return 0; } ``` 在此示例中,我们创建了一个 inotify 实例并将其添加到 epoll 实例中。然后,我们使用 inotify_add_watch 函数添加要监视的目录,并指定要监视的事件类型(在本例中为文件创建和删除事件)。最后,我们启动 epoll 循环,并等待事件发生。当事件发生时,我们读取 inotify 事件并处理它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值