标签: inotify

C 程序使用 inotify 监视多个目录以及子目录?

我有一个程序可以监视目录 ( /test) 并通知我。我想改进它以监视另一个目录(例如 /opt)。以及如何监视它的子目录,目前我会收到对/test. 但如果在 的子目录中进行更改,我不会收到任何通知/test,例如:

touch /test/sub-dir/files.txt
Run Code Online (Sandbox Code Playgroud)

这是我当前的代码 - 希望这会有所帮助

/*

Simple example for inotify in Linux.

inotify has 3 main functions.
inotify_init1 to initialize
inotify_add_watch to add monitor
then inotify_??_watch to rm monitor.you the what to replace with ??.
yes third one is  inotify_rm_watch()
*/


#include <sys/inotify.h>

int main(){
    int fd,wd,wd1,i=0,len=0;
    char pathname[100],buf[1024];
    struct inotify_event *event;

    fd=inotify_init1(IN_NONBLOCK);
    /* watch /test directory for any activity and report it back to me */
    wd=inotify_add_watch(fd,"/test",IN_ALL_EVENTS); …
Run Code Online (Sandbox Code Playgroud)

linux inotify

3
推荐指数
1
解决办法
7658
查看次数

获取有关 cgroup 流程更改的通知吗?

基本上,通常用于通知文件系统更改的 inotify 在 cgroup 虚拟文件系统中不起作用。

本质上,我想要一种在 cgroup 中的进程死亡或分叉时获得类似于 inotify 的通知的方法。我尝试将 inotify 附加到taskscgroup 文件系统内的虚拟文件,但是当进程自行分叉时,只有当 usespace 工具实际手动写入它以影响 cgroup 时,它才会执行任何操作。

linux inotify cgroups

3
推荐指数
1
解决办法
1778
查看次数

如何使用inotifywait查看文件夹而不是文件夹中的文件

我想使用 inotifyway 来监视文件夹中新创建或移动的文件,但仅限于文件。

假设我的文件夹名为“watched_folder_test”,我的文件名为“toto.txt”。如果我使用 mv 命令将文件移动到 watched_folder_test 我得到通知我想要

假设在 watched_folder_test 中,我有一个名为 foo 的文件夹,我创建了一个名为“bar.txt”的文件。我收到了我想要的通知。

但这是我的问题。如果我在 watch_folder_test 之外有一个文件夹名称 foo 并且我在其中有一个文件名 bar.txt ( foo/bar.txt ) 并且我将整个文件夹移动到 watch_folder_test 中。我只收到 foo 已创建的通知!没有关于 bar.txt 的内容。但是,我并不真正关心 foo,我只想知道“bar.txt”

到目前为止,这是我的代码

#!/bin/bash                                                                                          

inotifywait -mr /home/romain/depot/watched_folder_test -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        for ac in $action
        do
            isdir=`echo $ac | grep 'ISDIR'`
            if [ $? == 0 ]
            then
                echo "It's a folder"
            else
                echo "It's …
Run Code Online (Sandbox Code Playgroud)

bash ubuntu monitoring inotify inotifywait

3
推荐指数
1
解决办法
3405
查看次数

Inotify linux 监视子目录

是否可以以这种模式查看目录 /storage/data/usernames/Download/ -> /storage/data/*/Download/ 我需要查看是否在每个用户的下载文件夹中进行了更改。也许我需要创建所有路径的列表,将其放入数组并在每个文件夹上循环启动 inotify 进程,但这对系统来说可能很重。

linux bash monitoring inotify

3
推荐指数
1
解决办法
2242
查看次数

如何在WSL2中开启inotify?

inotify 在 WSL1 中工作。然后,由于 GNU 软件中不支持的功能,它在 WSL2 中被故意关闭,现已解决。

如何在 WSL2 中启用或打开 inotify?

inotify windows-subsystem-for-linux

3
推荐指数
1
解决办法
3347
查看次数

当文件被删除并再次创建时,inotify将停止监视文件

使用inotify时遇到一些问题.我使用inotify来监视文件的变化.这是我的代码:

int fd = inotify_init();
int wd = inotify_add_watch(fd, "/root/temp", IN_ALL_EVENTS);
int bufSize = 1000;
char *buf = new char[bufSize];
memset(buf, 0, sizeof(buf));
int nBytes = read(fd, buf, bufSize - 1);
cout << nBytes << " bytes read" << endl;
inotify_event *eventPtr = (inotify_event *)buf;
int offset = 0;
while (offset < nBytes)
{
    cout << eventPtr->mask << endl;
    offset += sizeof(inotify_event) + eventPtr->len;
    eventPtr = (inotify_event *)(buf + offset);
}
delete []buf;
Run Code Online (Sandbox Code Playgroud)

如果我删除"/ root/temp"并重新创建这样的文件,则inotify不会监视此文件的任何更改,这是怎么回事?谢谢.

c++ inotify

2
推荐指数
2
解决办法
3978
查看次数

使用inotify检测文件移动的问题.

我想使用inotify来监视目录的内容.在我尝试使用mv命令重命名目录中的文件之前,一切似乎都很好.我按预期得到了IN_MOVED_FROM但是没有IN_MOVED_TO.

以下是我的测试程序.编译:

g++ -Wall -o test test.cpp
Run Code Online (Sandbox Code Playgroud)

启动时间:

./test dir_to_watch
Run Code Online (Sandbox Code Playgroud)
#include <cstdio>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/inotify.h>

int main (int argc, char *argv[])
{
  int inotify_fd = inotify_init();

  if (inotify_fd < 0)
  {
    fprintf(stderr, "Unable to init inotify: %m\n");
    return 1;
  }

  int watch_descriptor = inotify_add_watch(inotify_fd,
                       argv[1],
                       IN_ALL_EVENTS);

  if (watch_descriptor < 0)
  {
    fprintf(stderr, "Unable to add inotify watch: %m\n");
    return 1;
  }

  union
  {
    inotify_event event;
    char pad[1024];
  }
  buffer;

  struct events …
Run Code Online (Sandbox Code Playgroud)

linux inotify

2
推荐指数
1
解决办法
4012
查看次数

对于手动编辑的文件,inotify IN_CLOSE_WRITE仅触发一次

我正在使用gevent和gevent_inotifyx运行python来监视文件以进行任何修改,使用IN_CLOSE_WRITE掩码等待文件在写入后关闭.我的活动是第一次被解雇但不是之后.我使用vim手动编辑文件.

使用不同的日志文件进行观察,并按预期工作.这是一个python日志文件,任何时候日志文件都会随着内容的变化而被触发.

有没有人遇到过这种情况?这可能是由于某些同步或刷新到磁盘?

python vim logging inotify

2
推荐指数
1
解决办法
928
查看次数

如何配置lsyncd监视文件更改的间隔?

如您所知,lsyncd可以监视本地文件更改并触发rsync以将文件更改同步到远程服务器。我的问题是如何配置lsyncd监视文件更改的间隔?
我没有在lsyncd.conf中找到任何参数,将不胜感激。

谢谢,
埃姆雷

linux rsync sync inotify

2
推荐指数
1
解决办法
4982
查看次数

inotify_add_watch 在 /sys/class/net/eth0/operstate 上失败

我在 Linux 中使用了 inotify,以便在网络接口链接发生变化时引发事件。每当接口链接更改时,/sys/class/net/eth40/operstate/ 文件都会被修改。但是在下面的代码片段中,即使文件被修改,读取功能仍处于阻塞状态。

#include <stdio.h>
#include <sys/inotify.h>
#include <stdlib.h>
#include <limits.h>
#include <signal.h>
#define FILE_TO_WATCH "/sys/class/net/eth40/operstate"
#define EVENT_SIZE (sizeof (struct inotify_event))
#define EVENT_BUFFER_LENGTH (1024 * EVENT_SIZE + NAME_MAX + 1)

void print_event(struct inotify_event *event) {

    int ret = 0;
    if (event->mask & IN_CREATE)
        printf("file created in directory\n");
    if (event->mask & IN_DELETE)
        printf("file deleted in directory\n");
    if (event->mask & IN_ACCESS)
        printf("file accessed\n");
    if (event->mask & IN_CLOSE)
        printf("file closed after reading or writing \n");
    if (event->mask & IN_OPEN)
        printf("file opened\n"); …
Run Code Online (Sandbox Code Playgroud)

c inotify

2
推荐指数
1
解决办法
2359
查看次数

python twisted INotify不阻塞反应器

我正在使用twsited的INotify监视/ dev目录以监视正在添加的新串行设备.我目前使用的代码类似于下面的代码.

notifier = INotify()
notifier.watch(FilePath("/dev"), IN_CREATE, callbacks=[self.created])
notifier.startReading()

def created(self, ignored, path, mask):
    ...
    blocking code
    ...
Run Code Online (Sandbox Code Playgroud)

我现在遇到的问题是当'created'被调用时,它阻塞了我的反应器,所以其他网络会话(我有同一个反应器的TCP和UDP连接)必须等待'created'方法完.

有谁知道如何让"创建"方法在后台运行,所以它不会阻止我的反应堆?

谢谢,

西蒙

python twisted inotify blocking

1
推荐指数
1
解决办法
1662
查看次数

使丢失的事件发声

我想监视系统上的USB密钥。我知道它们总是安装在/ media中,因此我使用inotify监视/ media。一些USB钥匙在插入时会创建一个文件夹(例如sda),直到拔出它们为止,而另一些会创建一个文件夹(例如sda),立即将其删除并创建一个新文件夹(例如sda1)。那是由于键上的分区。

但是,有时inotify仅捕获创建和删除第一个文件夹的事件,而错过第二个文件夹的创建。当我手动检查/ media时,第二个文件夹存在,但是inotify从未通知过它。

这种情况很少发生,并且一旦发生,总是在重新启动后首次插入设备。

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

/* size of the event structure, not counting name */
#define EVENT_SIZE  (sizeof (struct inotify_event))

/* reasonable guess as to size of 32 events */
#define BUF_LEN        (32 * (EVENT_SIZE + 16))

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

    fd = inotify_init();
    if (fd < 0) {
        perror("init failed");
        exit(EXIT_FAILURE);
    }

    wd = inotify_add_watch(fd,"/media",IN_ALL_EVENTS);
    if (wd …
Run Code Online (Sandbox Code Playgroud)

c linux inotify

1
推荐指数
1
解决办法
4757
查看次数

inotify 文件描述符上的 os.read:读取 32 字节有效但 31 引发异常

我正在编写一个应该使用 inotify 响应文件更改的程序。下面的骨架程序按我的预期工作......

# test.py
import asyncio
import ctypes
import os

IN_CLOSE_WRITE = 0x00000008

async def main(loop):
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    fd = libc.inotify_init()

    os.mkdir('directory-to-watch')
    wd = libc.inotify_add_watch(fd, 'directory-to-watch'.encode('utf-8'), IN_CLOSE_WRITE)
    loop.add_reader(fd, handle, fd)

    with open(f'directory-to-watch/file', 'wb') as file:
        pass

def handle(fd):
    event_bytes = os.read(fd, 32)
    print(event_bytes)

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
Run Code Online (Sandbox Code Playgroud)

...因为它输出...

b'\x01\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00file\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其更改为尝试读取 31 个字节...

b'\x01\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00file\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Run Code Online (Sandbox Code Playgroud)

......然后它引发了一个异常......

Traceback (most recent call last):
  File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/t.py", line 19, in handle
    event_bytes = os.read(fd, 31)
OSError: …
Run Code Online (Sandbox Code Playgroud)

linux ctypes inotify python-3.x python-asyncio

0
推荐指数
1
解决办法
133
查看次数