linux动态监听文件,linux shell 通过 inotify 实时监控文件的变化

inotify 是linux内核的一个特性,在内核 2.6.13 以上都可以使用。如果在shell环境下,可以安装 yum install inotify-tools,安装以后有两个命令可以用inotifywait 和 inotifywatch,inotifywait 是需要使用的命令。

这里监控一个文件 ~/test

#!/bin/bash

# inotify 监控文件的变化

inotifywait -mqr --timefmt '%Y-%m-%d %H:%M:%S' --format '%w %f %e %T' ~/test |

while read line; do

#echo $line

tmpfile=`echo $line | awk '{print $1}'`

tmptype=`echo $line | awk '{print $2}'`

tmpdate=`echo $line | awk '{print $3 " " $4 }'`

echo "file: "$tmpfile;

echo "type: "$tmptype;

echo "date: "$tmpdate;

done

演示的效果

帮助信息

Wait for a particular event on a file or set of files.

Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]

Options:

-h|--help Show this help text.

@ Exclude the specified file from being watched.

--exclude

Exclude all events on files matching the

extended regular expression .

--excludei

Like --exclude but case insensitive.

-m|--monitor Keep listening for events forever. Without

this option, inotifywait will exit after one

event is received.

-d|--daemon Same as --monitor, except run in the background

logging events to a file specified by --outfile.

Implies --syslog.

-r|--recursive Watch directories recursively.

--fromfile

Read files to watch from or `-' for stdin.

-o|--outfile

Print events to rather than stdout.

-s|--syslog Send errors to syslog rather than stderr.

-q|--quiet Print less (only print events).

-qq Print nothing (not even events).

--format Print using a specified printf-like format

string; read the man page for more details.

--timefmt strftime-compatible format string for use with

%T in --format string.

-c|--csv Print events in CSV format.

-t|--timeout

When listening for a single event, time out after

waiting for an event for seconds.

If is 0, inotifywait will never time out.

-e|--event [ -e|--event ... ]

Listen for specific event(s). If omitted, all events are

listened for.

Exit status:

0 - An event you asked to watch for was received.

1 - An event you did not ask to watch for was received

(usually delete_self or unmount), or some error occurred.

2 - The --timeout option was given and no events occurred

in the specified interval of time.

Events:

access file or directory contents were read

modify file or directory contents were written

attrib file or directory attributes changed

close_write file or directory closed, after being opened in

writeable mode

Events:

access file or directory contents were read

modify file or directory contents were written

attrib file or directory attributes changed

close_write file or directory closed, after being opened in

writeable mode

close_nowrite file or directory closed, after being opened in

read-only mode

close file or directory closed, regardless of read/write mode

open file or directory opened

moved_to file or directory moved to watched directory

moved_from file or directory moved from watched directory

move file or directory moved to or from watched directory

create file or directory created within watched directory

delete file or directory deleted within watched directory

delete_self file or directory was deleted

unmount file system containing file or directory unmounted

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux实时监控文件变化可以使用inotify工具。inotifyLinux内核提供的一种文件系统监控机制,可以监控文件或目录的变化,包括文件的创建、删除、修改、移动等操作。使用inotify可以实时监控文件变化,并在文件发生变化时进行相应的处理。可以通过编写脚本或使用现成的工具来实现对文件变化监控。 ### 回答2: Linux是一种非常常见且广泛使用的操作系统,但如果你需要实时监控文件变化,该怎么做呢?以下是一些常见的方法和工具: 1. inotify(内核提供的文件系统监控机制):inotify可以监视文件系统事件,如打开、关闭、创建和删除文件,这使得它成为一种非常流行的文件系统监控工具。通过使用inotify工具,您可以设置一个监控事件,并在文件被修改时立即接收通知。 2. auditd(Linux内置的审计机制):auditd是Linux内置的一个安全审计工具,它可以记录系统和应用程序的操作记录,并生成安全审计日志。通过配置auditd,您可以设置检测文件变化的规则,并在文件被修改时立即接收警报。 3. incron(inotify机制的增强版):incron可以使用inotify机制来监控文件变化,并在发生指定事件时执行相应操作。与inotify不同的是,incron可以执行更复杂的任务,如运行脚本、备份文件等,这使得它成为一种非常灵活和功能强大的文件监控工具。 4. lsof(列出打开文件):lsof可以列出当前打开的文件和进程,并显示文件的详细信息,如文件描述符、文件类型、文件大小等。通过使用lsof,您可以实时查看文件是否被打开、修改或关闭,这可以帮助您更好地监控文件变化。 总之,无论您是需要监控本地文件系统的变化,还是需要远程监控系统的文件变化,都可以使用上述工具和方法来实现实时监控文件变化。这样可以帮助您更好地掌握文件的状态和行为,从而更好地保护您的系统和数据。 ### 回答3: Linux系统中,可以使用inotify机制实现对文件实时监控inotifyLinux系统中一种文件事件通知机制,可以监控文件系统中指定目录或文件变化情况,并及时通知相关程序,实现文件实时监控功能。通过inotify可以监控文件系统中的各种事件,包括文件的创建、修改、删除、移动等操作。 使用inotify需要使用头文件<sys/inotify.h>,并调用inotify_init()函数来进行初始化。可以使用inotify_add_watch()函数来添加需要监控文件或目录,并指定需要监控的事件类型。当文件发生指定的事件时,inotify机制会生成相应的事件通知,并通过一个文件描述符返回给用户程序,以便进一步处理。 例如,以下代码可以实现监控当前目录下所有文件的修改事件: ``` #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/inotify.h> #define MAX_EVENTS 1024 #define EVENT_SIZE (sizeof (struct inotify_event)) #define EVENT_BUF_LEN (MAX_EVENTS * (EVENT_SIZE + 16)) int main() { int length, i = 0; int fd; int wd; char buffer[EVENT_BUF_LEN]; fd = inotify_init(); if (fd < 0) { perror("inotify_init"); } wd = inotify_add_watch(fd, ".", IN_MODIFY); while (1) { i = 0; length = read(fd, buffer, EVENT_BUF_LEN); if (length < 0) { perror("read"); } while (i < length) { struct inotify_event *event = (struct inotify_event *) &buffer[i]; if (event->len) { if (event->mask & IN_MODIFY) { printf("The file %s was modified.\n", event->name); } } i += EVENT_SIZE + event->len; } } (void) inotify_rm_watch(fd, wd); (void) close(fd); return 0; } ``` 该代码使用了inotify机制,监控当前目录下的所有文件,当文件被修改时,程序会输出相应的提示信息。在代码中,inotify_add_watch()函数用于添加需要监控的目录或文件,通过设置第三个参数可以指定需要监控的事件类型。while循环中的read()函数用于读取inotify机制返回的事件通知信息,程序根据读取到的信息进行相应的处理。 在Linux系统中,inotify机制可实现对文件实时监控和处理,可用于很多实际应用场景,如动态监控文件变化、自动同步文件等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值