Linux下如何知道文件被那个进程写

本文链接地址: Linux下如何知道文件被那个进程写

一个问题:(想想能解决不?)

“一个文件正在被进程写 我想查看这个进程 文件一直在增大 找不到谁在写 使用lsof也没找到”

这个问题挺有普遍性的,解决方法应该很多,这里我给大家提个比较直观的方法。

linux下每个文件都会在某个块设备上存放,当然也都有相应的inode, 那么透过vfs.write我们就可以知道谁在不停的写入特定的设备上的inode。
幸运的是systemtap的安装包里带了inodewatch.stp,位于/usr/local/share/doc/systemtap/examples/io目录下,就是用来这个用途的。
我们来看下代码:

$ cat inodewatch.stp   
#! /usr/bin/env stap  
probe vfs.write, vfs.read 
{  
  # dev and ino are defined by vfs.write and vfs.read  
  if (dev == MKDEV($1,$2) # major/minor device  
      && ino == $3)  
    printf ("%s(%d) %s 0x%x/%u\n",  
      execname(), pid(), probefunc(), dev, ino)  
}

这个脚本的使用方法如下: stap inodewatch.stp major minor ino

下面我们构造个场景: dd不停的写入一个文件,查出这个文件的ino, 以及它所在设备的major, minor, 运行stap脚本就可以得到答案。


场景交代好了,我们来演示下:

$ pwd 
/home/chuba  
$ df 
Filesystem           1K-blocks      Used Available Use% Mounted on  
...  
/dev/sdb1            1621245336 825209568 713681236  54% /home  
...  
$ ls -al /dev/sdb1  
brw-rw---- 1 root disk 8, 17 Oct 24 11:22 /dev/sdb1    
$ rm -f test.dat && dd if=/dev/zero of=test.dat  
^C9912890+0 records in 
9912890+0 records out  
5075399680 bytes (5.1 GB) copied, 26.8189 s, 189 MB/s

 

 

 

这个终端模拟文件的不停写入,同时在另外一个终端查验谁干的。这里我们已经知道设备的major/minor为8/17

$ stat -c '%i' test.dat 
25337884  
$ sudo stap /usr/local/share/doc/systemtap/examples/io/inodewatch.stp 8 17 25337884  
dd(740) vfs_write 0x800011/25337884  
dd(740) vfs_write 0x800011/25337884  
dd(740) vfs_write 0x800011/25337884  
dd(740) vfs_write 0x800011/25337884  
dd(740) vfs_write 0x800011/25337884  
dd(740) vfs_write 0x800011/25337884

 

 

看到了吧,dd是罪魁祸首,pid是740, 搞定收工!
小结: systemtap处理这种问题很是神器。

ps: 偶还木有做成功……估计与我机器有关……God bless.


求助喽:

June 25th, 2013 at 17:07 |

您好,从您博客中学习到这个方法,不过木有成功呀,求助:
1. 我的inodewatch.stp在不同的目录下
/usr/share/doc/systemtap-client-1.8/examples/io/inodewatch.stp
不过内容一致,应该是不影响的吧~

内核版本:2.6.32-279.14.1.el6.x86_64
我运行“rpm -qf /usr/bin/stap”得到的结果是:
systemtap-client-1.8-7.el6.x86_64
systemtap-devel-1.8-7.el6.x86_64

2. 然后根据您用法,我在boot分区随便建一个文件,并写入内容……
然后调用stap去找到哪个进程在写该文件!报错了……
[root@test boot]# stap /usr/share/doc/systemtap-client-1.8/examples/io/inodewatch.stp 8 1 25
semantic error: while resolving probe point: identifier ‘kernel’ at /usr/share/systemtap/tapset/vfs.stp:820:19
source: probe vfs.write = kernel.function(“vfs_write”)
^

semantic error: missing x86_64 kernel/module debuginfo under ‘/lib/modules/2.6.32-279.14.1.el6.x86_64/build’
semantic error: while resolving probe point: identifier ‘vfs’ at /usr/share/doc/systemtap-client-1.8/examples/io/inodewatch.stp:3:7
source: probe vfs.write, vfs.read
^

semantic error: no match
semantic error: while resolving probe point: identifier ‘kernel’ at /usr/share/systemtap/tapset/vfs.stp:752:18
source: probe vfs.read = kernel.function(“vfs_read”)
^

semantic error: while resolving probe point: identifier ‘vfs’ at /usr/share/doc/systemtap-client-1.8/examples/io/inodewatch.stp:3:18
source: probe vfs.write, vfs.read
^

Pass 2: analysis failed. Try again with another ‘–vp 01′ option.
Missing separate debuginfos, use: debuginfo-install kernel-2.6.32-279.14.1.el6.x86_64

正在http://sourceware.org/systemtap/ 继续学习中……
求指导: lin_credible@163.com. 或者直接回复,万分感谢~~

[Reply]

Yu Feng Reply:

系统的符号信息没安装好。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值