inotify 无法监控指定目录下新创建的文件
时间: 2023-12-15 15:03:33 浏览: 33
您好,针对您提出的问题,我可以给您提供以下信息和建议。
在Linux操作系统中,inotify确实无法直接监控新创建的文件,这是因为inotify只能监控已经存在的文件和目录。但是,您可以使用一些其他的方法来监控新创建的文件。以下是一些可能有用的方法:
1. 监控文件夹本身:您可以使用inotify监控整个文件夹,而不是特定的文件。这样,当新的文件被创建并添加到文件夹中时,inotify就会检测到。
2. 定时扫描:您可以编写一个脚本,定期扫描特定的目录,并检查是否有新的文件被创建。这种方法可能会导致性能问题,因为它需要定期扫描整个目录树。
3. 将文件写入管道:您可以使用一个管道,将新创建的文件的内容写入该管道中。然后,您可以使用inotify监控管道,以检测新的文件。
希望这些信息可以对您有所帮助。如果您有任何其他问题或需要进一步的帮助,请随时问我。
相关问题
使用epoll + inotify监控文件
要使用 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 事件并处理它。
inotify机制 文件创建检测
inotify是Linux内核提供的一种文件系统通知机制,可以用来监视文件系统的变化。其中包括文件的创建、删除、修改、移动等操作。使用inotify机制可以非常方便地实现对文件系统变化的监控,比如实时同步文件夹、监控日志文件等应用场景。
对于文件的创建检测,我们可以使用inotify机制中的IN_CREATE事件来实现。当一个新文件被创建时,内核会向监听该目录的进程发送一个IN_CREATE事件通知。进程可以通过inotify API来获取该事件通知,并进行相应的处理。
以下是一个使用inotify机制进行文件创建监控的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE (sizeof(struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + 16))
int main(int argc, char **argv)
{
int fd, wd;
char buf[BUF_LEN];
ssize_t len;
struct inotify_event *event;
if(argc < 2){
fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = inotify_init();
if(fd == -1){
perror("inotify_init");
exit(EXIT_FAILURE);
}
wd = inotify_add_watch(fd, argv[1], IN_CREATE);
if(wd == -1){
perror("inotify_add_watch");
exit(EXIT_FAILURE);
}
printf("Watching '%s' for new files...\n", argv[1]);
while(1){
len = read(fd, buf, BUF_LEN);
if(len == -1 && errno != EAGAIN){
perror("read");
exit(EXIT_FAILURE);
}
if(len <= 0)
continue;
event = (struct inotify_event *)buf;
while(len > 0){
if(event->mask & IN_CREATE){
printf("New file '%s' created.\n", event->name);
}
len -= EVENT_SIZE + event->len;
event = (struct inotify_event *)((char *)event + EVENT_SIZE + event->len);
}
}
exit(EXIT_SUCCESS);
}
```
运行该程序时,需要指定要监控的目录作为命令行参数,例如:
```
./inotify_test /tmp
```
该程序会不断地监控指定目录下的文件创建事件,并在控制台输出新文件的名称。