linux文件监控|13、linux上怎么动态监控一个文件的内容变化

linux文件监控|13、linux上怎么动态监控一个文件的内容变化的第1张示图

⑴ linux 监控文件被什么进程修改

你好,如果只是监视普通文件操作,一般情况下不需要修改内核专。 运行“strace -e file -o log.txt 可执行文件属名" 即可。对已经运行的程序,用strace -p pid。 最创建子进程的,加-f参数。具体man strace。

⑵ 在linux下使用inotify监控,能不能够知道监控目录下子目录中是哪个文件被修改了。。。求方法。。。

这个我们期末考试考过。inotify只能监控单层目录变化,不能监控子目录中的变化情况。如果需要监控子目录,需要在调用inotify_add_watch(int fd, char *dir, int mask):int建立监控时,递归建立子目录的监控,伪代码如下void addwatch(int fd, char *dir, int mask){ wd = inotify_add_watch(fd, dir, mask); 向目录集合加入(wd, dir); for (dir下所有的子目录subdir) addwatch(fd, subdir, mask);}这样就可以得到一个目录集合,其中每一个wd对应一个子目录。当你调用read获取信息时,可以得到一个下面的结构体struct inotify_event{ int wd; /* Watch descriptor. */ uint32_t mask; /* Watch mask. */ uint32_t cookie; /* Cookie to synchronize two events. */ uint32_t len; /* Length (including NULs) of name. */ char name __flexarr; /* Name. */};其中,通过event->wd和刚才记录的目录集合可以知道变动的具体子目录。event->name为具体的文件名称。event->name是一个char name[0]形式的桩指针,具体的name占据的长度可以由event->len得出我的监控部分代码如下:enum {EVENT_SIZE = sizeof(struct inotify_event)};enum {BUF_SIZE = (EVENT_SIZE + 16) << 10};void watch_mon(int fd){ int i, length; void *buf; struct inotify_event *event; buf = malloc(BUF_SIZE); while ((length = read(fd, buf, BUF_SIZE)) >= 0) { i = 0; while (i < length) { event = buf + i; if (event->len) 具体处理函数(event); i += EVENT_SIZE + event->len; } } close(fd); exit(1);}在你的具体处理函数中,通过wd辨识子目录,通过name辨识文件 这是利用C++STLmap写的一个范例,可以监视当前目录下(含子目录)的变化,创建,删除过程(新建立的目录不能监视,只能通过监视到创建新目录的事件后重新初始化监视表)新版1.1.0,可以监视创建的子目录,方法是,当do_action探测到新目录创建的动作时,调用inotify_add_watch追加新的监视/* Copyright (C) 2010-2011 LIU An ([email protected]) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <sys/inotify.h>#include <errno.h>#include <dirent.h>#include <map>#include <string>using namespace std;void addwatch(int, char*, int);static int filter_action(uint32_t mask);int watch_init(int mask, char *root);void addwatch(int fd, char *dir, int mask);static void do_action(int fd, struct inotify_event *event);void watch_mon(int fd);static void send_mess(char *name, char *act, int ewd);void append_dir(int fd, struct inotify_event *event, int mask);map<int, string> dirset;enum{MASK = IN_MODIFY | IN_CREATE | IN_DELETE};int main(int argc, char **argv){ int fd; if (argc != 2) { fprintf(stderr, "Usage: %s dir\n", argv[0]); exit(1); } fd = watch_init(MASK, argv[1]); watch_mon(fd); return 0;}int watch_init(int mask, char *root){ int i, fd; if ((fd = inotify_init()) < 0) perror("inotify_init"); addwatch(fd, root, mask); return fd;}void addwatch(int fd, char *dir, int mask){ int wd; char subdir[512]; DIR *odir; struct dirent *dent; if ((odir = opendir(dir)) == NULL) { perror("fail to open root dir"); exit(1); } wd = inotify_add_watch(fd, dir, mask); dirset.insert(make_pair(wd, string(dir))); errno = 0; while ((dent = readdir(odir)) != NULL) { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; if (dent->d_type == DT_DIR) { sprintf(subdir, "%s/%s", dir, dent->d_name); addwatch(fd, subdir, mask); } } if (errno != 0) { perror("fail to read dir"); exit(1); } closedir (odir);}enum {EVENT_SIZE = sizeof(struct inotify_event)};enum {BUF_SIZE = (EVENT_SIZE + 16) << 10};void watch_mon(int fd){ int i, length; void *buf; struct inotify_event *event; buf = malloc(BUF_SIZE); while ((length = read(fd, buf, BUF_SIZE)) >= 0) { i = 0; while (i < length) { event = (struct inotify_event*)(buf + i); if (event->len) do_action(fd, event); i += EVENT_SIZE + event->len; } } close(fd); exit(1);}static char action[][10] ={ "modified", "accessed", "created", "removed"};enum{NEWDIR = IN_CREATE | IN_ISDIR};static void do_action(int fd, struct inotify_event *event){ int ia, i; if ((ia = filter_action(event->mask)) < 0) return; if ((event->mask & NEWDIR) == NEWDIR) append_dir(fd, event, MASK); send_mess(event->name, action[ia], event->wd);}void append_dir(int fd, struct inotify_event *event, int mask){ char ndir[512]; int wd; sprintf(ndir, "%s/%s", dirset.find(event->wd)->second.c_str(), event->name); wd = inotify_add_watch(fd, ndir, mask); dirset.insert(make_pair(wd, string(ndir)));}static int filter_action(uint32_t mask){ if (mask & IN_MODIFY) return 0; if (mask & IN_ACCESS) return 1; if (mask & IN_CREATE) return 2; if (mask & IN_DELETE) return 3; return -1;}static void send_mess(char *name, char *act, int ewd){ char format[] = "%s was %s.\n"; char file[512]; sprintf(file, "%s/%s", dirset.find(ewd)->second.c_str(), name); printf(format, file, act);}参考资料是我们作业的提交,没有考虑递归创建子目录监控的问题。

⑶ linux中用什么命令实现对目录文件变化的监控

|#!/bin/bashif [ ! -f check.txt ];then ls >check.txtelif [ ! -f check.old ];then mv check.txt check.old ls >check.txt diff check.txt check.old|grep '^>'|sed 's/>/deleted file:/g' diff check.txt check.old|grep '^<'|sed 's/</created file:/g'else mv check.txt check.old ls >check.txt diff check.txt check.old|grep '^>'|sed 's/>/deleted file:/g' diff check.txt check.old|grep '^<'|sed 's/</created file:/g'fi不知是不是你想要的。。。只实现监控新增的文内件和容删除的文件。

⑷ java如何实现linux下实时监控文件是否有变化

java 的WatchService 类提供了一种方式可以检查try{WatchService watchService = FileSystems.getDefault().newWatchService();Path path = Paths.get(pathName);// 注册监听器path.register(watchService,StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE);while (true){// 阻塞方式,消费文件回更改事件答List<WatchEvent<?>> watchEvents = watchService.take().pollEvents();for (WatchEvent<?> watchEvent : watchEvents){System.out.printf("[%s]文件发生了[%s]事件。%n", watchEvent.context(), watchEvent.kind());}}}catch (Exception e){}

⑸ LINUX下,如何监控文件,得到新写入文件的内容 一个文件不断被写入,要

看tail -f的实现.(coreutils)

⑹ 在linux系统下,编写一个shell脚本 实现对文件的监控

如果文件比较小,你可以备份原文件用diff来对比判断。因为不知道你具体是想通过什么专对比,不好属说。你可以通过tripwire这个软件来检测,具体安装方法自己网络,不细说了。如果自己写你必须要找到一些判断内容,比如时间,容量,如果文件包含时间戳你就可以判断时间戳来截取文件内容。自己写的话就需要具体情况具体分析。

⑺ 13、linux上怎么动态监控一个文件的内容变化

用途说明tail命令可以输出文件的尾部内容,默认情况下它显示文件的最后十行。它常用来动态监视文件的尾部内容的增长情况,比如用来监视日志文件的变化。与tail命令对应的是head命令,用来显示文件头部内容。 常用参数格式:tail file输出指定文件file的尾部内容,默认输出最后十行内容(outputthe last part of files。Print the last 10 lines of each FILE tostandard output. ) 格式:tail file1 file2…指定多个文件时,会显示每个文件的文件名称,再显示该文件的尾部内容(Withmore than one FILE, precede each with a header giving the file name.) 格式:tail格式:tail -不指定文件时,表明从标准输入读取内容,这通常用在管道线后面,把前一个命令的输出作为tail的输入内容(Withno FILE, or when FILE is -, read standard input.) 格式:tail -n file格式:tail -n n file格式:tail –lines=n显示文件最后n 行,比如tail -20 file就是显示文件最后10行,这个参数可以配合其他参数与使用。注意上面三种格式的斜体n 是实际要显示的行数的数值。 注意:tail-n可以显示最后n行的文本内容。那么有没有一种方式显示从n行开始的文本内容,答案是肯定的。tail -n +4file表示显示文件file从第4行开始的内容。从1开始计数。 格式:tail -f file动态跟踪文件file的增长情况(outputappended data as the filegrows),tail会每隔一秒去检查一下文件是否增加新的内容,如果增加就追加在原来的输出后面显示。但这种情况,必须保证在执行tail命令时,文件已经存在。如果想终止tail-f的输出,按Ctrl+C中断tail程序即可。如果按Ctrl+C不能中断输出,那么可以在别的终端上执行killall tail强行终止。 注意:采用tail-f来监控文件变化情况时,在某些情况会不太灵。比如在Java应用程序中采用log4j日志时,每隔1个小时生成一个新的日志文件,当前的日志输出在 LOG4J.LOG中,当一个小时过去后,log4j会将LOG4J.LOG改名成LOG4J.yyyy-mm-dd-HH的形式。那么这个时候tail -f就不能动态输出新的日志内容了。tail命令本身提供了很多参数,似乎都不能完美的解决这个问题。最后只好编写了一个脚本ftail.sh来跟踪日 志,详见《Linux下实时跟踪log4j日志文件的bash脚本 – 增强了tail -f的功能 》。刚才我仔细查看了tail的手册页,发现tail -F就能够做到跟踪这种类型的日志。转念一想,这种需求应该早就被Linux世界的人给满足了的。 格式:tail -F file格式:tail–follow=name –retry file功能与tail -ffile相同,也是动态跟踪文件的变化,不同的是执行此命令时文件可以不存在。 以上处理都是针对文本文件的,下面是针对二进制文件的情形。 格式:tail -c n file取文件file的最后n个字节。 格式:tail -c +n file取文件file的第n个字节后的内容。从1开始计数。使用示例示例一 输出文件尾部先使用seq命令输出20个数字保存到1.txt,然后尝试使用tail命令。[[email protected] ~]# seq 20 >1.txt[[email protected] ~]# cat 1.txt1234567891011121314151617181920[[email protected] ~]# tail 1.txt11121314151617181920[[email protected] ~]# tail -3 1.txt181920[[email protected] ~]# tail -n 3 1.txt181920[[email protected] ~]# tail –lines=3 1.txt181920[[email protected] ~]# tail -n +14 1.txt14151617181920[[email protected] ~]# 示例二 动态跟踪tomcat输出动态跟踪tomcat输出。[[email protected] logs]# tail -f catalina.out at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619)2010-12-0313:23:02,236 [http-80-15] DEBUG mhr.roi.MhrManager -MhrGetJobReq={seq=991,job_id='86130469-0006'}2010-12-0313:23:02,301 [http-80-15] DEBUG mhr.roi.MhrManager -MhrGetJobRsp={seq=991,result=0(成功),,info={job_id='86130469-0006',employer_id=86130469,employer_name=无锡富士时装有限公司,,,job_title='|570309|',job_title0='文员',job_type=f(全 职),issue_time='2010-11-0300:00:00.0',work_address='1902',work_address0=无锡 市,desired_count='1',,,,,,job_desc=大专,1年以上5s管理工作经验,电脑操作熟练。,required_experience=1(一年以上),,,,required_degree=15(大专),,,,,,,valid_days=30,access_count=12,expire_time='2010-12-0300:00:00.0',job_status=1(过期),,,,,,contact_name=王小 姐,contact_number=0510-85550088,remarks='★此职位是从后台导入的',enter_time='2010-11-0309:45:11.0',change_time=2010-12-0302:18:05.0,,job_seq=123201,,required_min_age=22,,,accommodations=面议,serve_principal=wjw12580,job_summary=大专,1年以上5s管理工作经验,电脑操作熟练,,}}2010-12-0313:23:02,302 [http-80-15] DEBUG mhr.roi.MhrManager -MhrGetEmployerReq={seq=0,employer_id='86130469'}2010-12-0313:23:02,304 [http-80-15] DEBUG mhr.roi.MhrManager -MhrGetEmployerRsp={seq=0,result=0(成功),,info= {employer_id='86130469',employer_name=无锡富士时装有限公司,employer_region=1902,employer_address=无锡市滨湖镇山水城科技园8号,,employer_desc=无锡 富士时装(集团)有限公司成立于1992年,是中日韩合资企业。主要生产高档针织时装,产品全部外销,连续多年荣获“中国行业500强”、“全国工业重点 行业效益十佳企业”、“无锡市百佳企业”等称号。公司总部位于江苏省无锡市滨湖镇山水城科技园,全新的厂房设施,占地面积30亩。公司分别在苏州、泰兴、 盐城、徐州设有分厂,集团公司现有职工1500多人,年销售额近3亿元。,,,,,,open_mode=5(系统自动操 作),open_time='2010-11-0309:45:10.0',,,,,contact_name=王小 姐,contact_number=0510-85550088,,,,,employer_status=1(已经开通),,,login_password=871386,,agency=false,balance=100.0000,,,,,serve_principal=wjw12580,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,remarks='此帐号由“12580zgz-盐城维克多网络传媒有限公司”导入',enter_time='2010-11-03 09:45:10.0',}}Ctrl+C[[email protected] logs]# 示例三 动态跟踪log4j日志经过我的试验,发现tail -F功能的强大,它等同于–follow=name–retry。如果你跟踪的文件被移动或者改名后, 你还想继续tail它, 你可以使用这个选项。tail手册页中关于–retry的说明:keep trying to open a file even if itis inaccessible when tail starts or if it becomes inaccessible later; useful when following by name, i.e., with –follow=name。 tail命令开始执行时文件不存在或者执行过程中文件不能访问,会不断重试。关于–follow的说明:-f, –follow[={name|descriptor}]output appended data as the file grows; -f, –follow, and –follow=descriptorare equivalent 。–follow=descriptor表明跟踪的是文件描述符, –follow=name表明跟踪的是文件名称。 如果文件名称改掉之后,还想继续跟踪原文件名称对应的尾部内容,就得使用-F选项而不是-f选项了。[[email protected]_server]# tail -F log/IMX.LOG14:13:28.892 INFO ImxConnection[6] imx.server.ImxConnection – RXIMX_ACTIVE_TEST{seq=3460,client_id=1291343201649042,presence_status=1(presence_status_online),}14:13:28.892 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB – 006417 (01/02/00) -Connection #9 served14:13:28.892 INFO ImxConnection[6] imx.dbo.ImxOnlineInfoRow – EXEC SQL UPDATEimx_online_info SET last_active_time = '2010-12-03 14:13:28.0' WHERE account ='zhy'14:13:28.894 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB – UPDATE imx_online_info SETlast_active_time = '2010-12-03 14:13:28.0' WHERE account = 'zhy'; (1milliseconds)14:13:28.894 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB – 006417 (00/02/00) -Connection #9 returned (now AVAILABLE)14:13:29.625 INFO ImxConnection[6] imx.server.ImxConnection – RXIMX_ACTIVE_TEST{seq=3461,client_id=1291343201649042,presence_status=1(presence_status_online),}14:13:29.626 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB – 006418 (01/02/00) -Connection #8 served14:13:29.626 INFO ImxConnection[6] imx.dbo.ImxOnlineInfoRow – EXEC SQL UPDATEimx_online_info SET last_active_time = '2010-12-03 14:13:29.0' WHERE account ='zhy'14:13:29.627 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB – UPDATE imx_online_info SETlast_active_time = '2010-12-03 14:13:29.0' WHERE account = 'zhy'; (0milliseconds)14:13:29.653 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB – 006418 (00/02/00) -Connection #8 returned (now AVAILABLE)Ctrl+C[[email protected]_server]#

⑻ 在linux中实现操作系统的文件使用率的监控如果发现有文件系统>=80%记录当前时间及具体文件名称和使用率

这个好像有系统监控软件的。。不用费劲写脚本的!!!gnome什么的软件、、、、

⑼ linux文件系统监控工具

Nagios。。

⑽ Linux如何监控目录下的文件情况,一旦有新文件就以硬链接的方式复制到其他目录

#! /bin/bash$FILE_LIST=$(find . -type f -cmin -5 -maxdepth 1)for file in ${FILE_LIST} ;do ln -f ${file} ~/tmp/${file}done ##其中find命令是核心##-type f表示只查找普通文件##-cmin -5表示只查找5分钟之内创建的文件##-maxdepth 1表示查找的目录深度,1表示只查找当前目录,如果不指定-maxdepth将递归查找##ln -f ${file} ~/tmp/${file}是将查找到的文件硬链接到~/tmp目录##你可以根据自己的需要再做相应的修改

未经允许不得转载:山九号 » linux文件监控|13、linux上怎么动态监控一个文件的内容变化

赞 (0)