提示: 该脚本与 lsof 命令功能类似
1.搜索进程打开的句柄列表:
for i in `find /proc -maxdepth 2 -name fd`; do echo "$i" && ls -al $i ; done
2.提取 /proc/ 与 /fd 之间 进程号:
sed -r 's/.*\/proc\/(.*)\/fd.*/\1/'
3.替换命令:
sed 's/AND/_and_/g'
4.打印列:
awk '{print $2}'
5.过滤 total 字符串
grep -v total
6.过滤目录(. 及 .. 等为目录符)
grep -v dr\-x
7.综合命令:
for i in `find /proc -maxdepth 2 -name fd`;
do echo "======================="
&&
cat ` echo "$i" | sed 's/fd/stat/g' ` | awk '{print $2}'
&& ls -al $i | grep -v total | grep -v dr\-x;
done
7.1 执行结果
=======================
(init)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/console
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/console
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/console
=======================
(kthreadd)
=======================
(ksoftirqd/0)
=======================
(kworker/0:0)
=======================
(kworker/u:0)
=======================
(khelper)
=======================
(kworker/u:1)
=======================
(sync_supers)
=======================
(bdi-default)
=======================
(kintegrityd)
=======================
(kblockd)
=======================
(spi0)
=======================
(spi1)
=======================
(khubd)
=======================
(kusbotg)
=======================
(rpciod)
=======================
(kworker/0:1)
=======================
(kswapd0)
=======================
(fsnotify_mark)
=======================
(nfsiod)
=======================
(romblock0)
=======================
(mtdblock0)
=======================
(romblock1)
=======================
(mtdblock1)
=======================
(romblock2)
=======================
(mtdblock2)
=======================
(romblock3)
=======================
(mtdblock3)
=======================
(kpsmoused)
=======================
(cfinteractive)
=======================
(deferwq)
=======================
(jffs2_gcd_mtd2)
=======================
(udevd)
lrwx------ 1 root root 64 Jan 1 1970 0 -> /dev/null
lrwx------ 1 root root 64 Jan 1 1970 1 -> /dev/null
lrwx------ 1 root root 64 Jan 1 1970 2 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /dev/.udev/queue.bin
lrwx------ 1 root root 64 Mar 23 15:29 4 -> socket:[31]
lrwx------ 1 root root 64 Mar 23 15:29 5 -> socket:[32]
lr-x------ 1 root root 64 Mar 23 15:29 6 -> anon_inode:inotify
lrwx------ 1 root root 64 Mar 23 15:29 7 -> anon_inode:[signalfd]
lrwx------ 1 root root 64 Mar 23 15:29 8 -> socket:[33]
lrwx------ 1 root root 64 Mar 23 15:29 9 -> socket:[34]
=======================
(udevd)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 10 -> socket:[149]
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/null
lr-x------ 1 root root 64 Mar 23 15:29 6 -> anon_inode:inotify
lrwx------ 1 root root 64 Mar 23 15:29 9 -> socket:[34]
=======================
(udevd)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 10 -> socket:[155]
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/null
lr-x------ 1 root root 64 Mar 23 15:29 6 -> anon_inode:inotify
lrwx------ 1 root root 64 Mar 23 15:29 9 -> socket:[34]
=======================
(jffs2_gcd_mtd3)
=======================
(hidog)
=======================
(RtmpTimerTask)
=======================
(RtmpMlmeTask)
=======================
(RtmpCmdQTask)
=======================
(RtmpWscTask)
=======================
(wpa_supplicant)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 4 -> socket:[802]
lrwx------ 1 root root 64 Mar 23 15:29 5 -> socket:[803]
lrwx------ 1 root root 64 Mar 23 15:29 6 -> socket:[808]
lrwx------ 1 root root 64 Mar 23 15:29 7 -> socket:[809]
=======================
(wifi_config)
lr-x------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/console
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/console
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /mnt/mtd/firmware/log/wifi_config.log
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:29 5 -> /mnt/mtd/firmware/log/system.log
lrwx------ 1 root root 64 Mar 23 15:29 6 -> socket:[835]
lrwx------ 1 root root 64 Mar 23 15:29 7 -> socket:[836]
lrwx------ 1 root root 64 Mar 23 15:29 8 -> socket:[837]
=======================
(sh)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/ttyS000
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/ttyS000
lrwx------ 1 root root 64 Mar 23 15:29 10 -> /dev/tty
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/ttyS000
=======================
(telnetd)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 3 -> socket:[818]
lrwx------ 1 root root 64 Mar 23 15:29 4 -> socket:[1308]
lrwx------ 1 root root 64 Mar 23 15:29 5 -> /dev/ptmx
lrwx------ 1 root root 64 Mar 23 15:29 6 -> socket:[1325]
lrwx------ 1 root root 64 Mar 23 15:29 7 -> /dev/ptmx
=======================
(flush-mtd-unmap)
=======================
(udhcpc)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/null
l-wx------ 1 root root 64 Mar 23 15:29 10 -> pipe:[1263]
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /mnt/mtd/firmware/log/wifi_config.log
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:29 5 -> /mnt/mtd/firmware/log/system.log
lrwx------ 1 root root 64 Mar 23 15:29 6 -> socket:[835]
lrwx------ 1 root root 64 Mar 23 15:29 7 -> socket:[836]
lrwx------ 1 root root 64 Mar 23 15:29 8 -> socket:[837]
lr-x------ 1 root root 64 Mar 23 15:29 9 -> pipe:[1263]
=======================
(sh)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 10 -> /dev/tty
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/pts/0
=======================
(sh)
lrwx------ 1 root root 64 Mar 23 15:29 0 -> /dev/pts/1
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/pts/1
lrwx------ 1 root root 64 Mar 23 15:29 10 -> /dev/tty
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/pts/1
=======================
(loader)
lr-x------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /mnt/mtd/firmware/log/loader.log
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:29 5 -> /mnt/mtd/firmware/log/system.log
l-wx------ 1 root root 64 Mar 23 15:29 6 -> /dev/watchdog
=======================
(NV_Updater)
lr-x------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /mnt/mtd/firmware/log/system.log
lrwx------ 1 root root 64 Mar 23 15:29 5 -> socket:[1643]
=======================
(loader)
lr-x------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /mnt/mtd/firmware/log/loader.log
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:29 5 -> /mnt/mtd/firmware/log/system.log
l-wx------ 1 root root 64 Mar 23 15:29 6 -> /dev/watchdog
=======================
(flashpolicyd)
lr-x------ 1 root root 64 Mar 23 15:29 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:29 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 2 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:29 3 -> socket:[1683]
=======================
(NV_CAM_NETVUE3_)
lr-x------ 1 root root 64 Mar 23 15:25 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:25 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:25 10 -> /dev/vb
lrwx------ 1 root root 64 Mar 23 15:25 11 -> /dev/sys
lr-x------ 1 root root 64 Mar 23 15:25 12 -> /dev/isp_dev
lr-x------ 1 root root 64 Mar 23 15:25 13 -> /dev/isp_dev
lrwx------ 1 root root 64 Mar 23 15:25 14 -> /dev/mem
lrwx------ 1 root root 64 Mar 23 15:25 15 -> /dev/i2c-0
lr-x------ 1 root root 64 Mar 23 15:25 16 -> /dev/vi
lr-x------ 1 root root 64 Mar 23 15:25 17 -> /dev/vi
lr-x------ 1 root root 64 Mar 23 15:25 18 -> /dev/vi
lr-x------ 1 root root 64 Mar 23 15:25 19 -> /dev/vpss
lrwx------ 1 root root 64 Mar 23 15:25 2 -> /dev/pts/0
lr-x------ 1 root root 64 Mar 23 15:25 20 -> /dev/vpss
lr-x------ 1 root root 64 Mar 23 15:25 21 -> /dev/vpss
lr-x------ 1 root root 64 Mar 23 15:25 22 -> /dev/vpss
lr-x------ 1 root root 64 Mar 23 15:25 23 -> /dev/vpss
lrwx------ 1 root root 64 Mar 23 15:25 24 -> /dev/ai
lrwx------ 1 root root 64 Mar 23 15:25 25 -> /dev/ao
lrwx------ 1 root root 64 Mar 23 15:25 26 -> /dev/mmz_userdev
lrwx------ 1 root root 64 Mar 23 15:25 27 -> /dev/aenc
lr-x------ 1 root root 64 Mar 23 15:25 28 -> /dev/vi
lrwx------ 1 root root 64 Mar 23 15:25 29 -> /dev/mmz_userdev
lrwx------ 1 root root 64 Mar 23 15:25 3 -> /mnt/mtd/firmware/log/loader.log
lr-x------ 1 root root 64 Mar 23 15:25 30 -> /dev/ive
lrwx------ 1 root root 64 Mar 23 15:25 31 -> /dev/NV_Driver
lrwx------ 1 root root 64 Mar 23 15:25 32 -> /dev/hi_rtc
lrwx------ 1 root root 64 Mar 23 15:25 33 -> socket:[1784]
lrwx------ 1 root root 64 Mar 23 15:25 34 -> socket:[1786]
lrwx------ 1 root root 64 Mar 23 15:25 35 -> socket:[1788]
lrwx------ 1 root root 64 Mar 23 15:25 36 -> socket:[1801]
lrwx------ 1 root root 64 Mar 23 15:25 37 -> socket:[1802]
lrwx------ 1 root root 64 Mar 23 15:25 38 -> socket:[1803]
lrwx------ 1 root root 64 Mar 23 15:25 39 -> socket:[1816]
lrwx------ 1 root root 64 Mar 23 15:25 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:25 40 -> socket:[1817]
lrwx------ 1 root root 64 Mar 23 15:25 41 -> socket:[1818]
lrwx------ 1 root root 64 Mar 23 15:25 42 -> socket:[1819]
lrwx------ 1 root root 64 Mar 23 15:25 43 -> socket:[1820]
lrwx------ 1 root root 64 Mar 23 15:27 44 -> /dev/venc
lrwx------ 1 root root 64 Mar 23 15:27 45 -> /dev/venc
lr-x------ 1 root root 64 Mar 23 15:27 46 -> /dev/rgn
lrwx------ 1 root root 64 Mar 23 15:25 5 -> /mnt/mtd/firmware/log/system.log
l-wx------ 1 root root 64 Mar 23 15:25 6 -> /dev/watchdog
lrwx------ 1 root root 64 Mar 23 15:25 7 -> /mnt/mtd/firmware/log/mainapp.log
lrwx------ 1 root root 64 Mar 23 15:25 8 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:25 9 -> /mnt/mtd/firmware/log/system.log
=======================
(NV_CAM_NETVUE3_)
lr-x------ 1 root root 64 Mar 23 15:25 0 -> /dev/null
lrwx------ 1 root root 64 Mar 23 15:25 1 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:25 10 -> socket:[1730]
lrwx------ 1 root root 64 Mar 23 15:25 11 -> socket:[4702]
lrwx------ 1 root root 64 Mar 23 15:25 2 -> /dev/pts/0
lrwx------ 1 root root 64 Mar 23 15:25 3 -> /mnt/mtd/firmware/log/loader.log
lrwx------ 1 root root 64 Mar 23 15:25 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:25 5 -> /mnt/mtd/firmware/log/system.log
l-wx------ 1 root root 64 Mar 23 15:25 6 -> /dev/watchdog
lrwx------ 1 root root 64 Mar 23 15:25 7 -> /mnt/mtd/firmware/log/mainapp.log
lrwx------ 1 root root 64 Mar 23 15:25 8 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:25 9 -> /mnt/mtd/firmware/log/system.log
=======================
(tail)
lrwx------ 1 root root 64 Mar 23 15:26 0 -> /dev/pts/1
lrwx------ 1 root root 64 Mar 23 15:26 1 -> /dev/pts/1
lrwx------ 1 root root 64 Mar 23 15:26 2 -> /dev/pts/1
lr-x------ 1 root root 64 Mar 23 15:26 3 -> /tmp/run_info.log
=======================
cat: can't open '/proc/2926/stat': No such file or directory
ls: /proc/2926/fd: No such file or directory
8.拓展命令:
搜索某个文件句柄被哪些进程所打开。
例如: /tmp/run_info.log 文件
for i in `find /proc -maxdepth 2 -name fd`;
do echo "======================="
&&
cat ` echo "$i" | sed 's/fd/stat/g' ` | awk '{print $2}'
&& ls -al $i | grep -v total | grep -v dr\-x | grep "run_info.log";
done
在 综合命令的基础上 加上 | grep “run_info.log” 就可以了
8.1 执行结果:
=======================
(init)
=======================
(kthreadd)
=======================
(ksoftirqd/0)
=======================
(kworker/0:0)
=======================
(kworker/u:0)
=======================
(khelper)
=======================
(kworker/u:1)
=======================
(sync_supers)
=======================
(bdi-default)
=======================
(kintegrityd)
=======================
(kblockd)
=======================
(spi0)
=======================
(spi1)
=======================
(khubd)
=======================
(kusbotg)
=======================
(rpciod)
=======================
(kworker/0:1)
=======================
(kswapd0)
=======================
(fsnotify_mark)
=======================
(nfsiod)
=======================
(romblock0)
=======================
(mtdblock0)
=======================
(romblock1)
=======================
(mtdblock1)
=======================
(romblock2)
=======================
(mtdblock2)
=======================
(romblock3)
=======================
(mtdblock3)
=======================
(kpsmoused)
=======================
(cfinteractive)
=======================
(deferwq)
=======================
(jffs2_gcd_mtd2)
=======================
(udevd)
=======================
(udevd)
=======================
(udevd)
=======================
(jffs2_gcd_mtd3)
=======================
(hidog)
=======================
(RtmpTimerTask)
=======================
(RtmpMlmeTask)
=======================
(RtmpCmdQTask)
=======================
(RtmpWscTask)
=======================
(wpa_supplicant)
=======================
(wifi_config)
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
=======================
(sh)
=======================
(telnetd)
=======================
(flush-mtd-unmap)
=======================
(udhcpc)
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
=======================
(sh)
=======================
(sh)
=======================
(loader)
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
=======================
(NV_Updater)
lrwx------ 1 root root 64 Mar 23 15:29 3 -> /tmp/run_info.log
=======================
(loader)
lrwx------ 1 root root 64 Mar 23 15:29 4 -> /tmp/run_info.log
=======================
(flashpolicyd)
=======================
(NV_CAM_NETVUE3_)
lrwx------ 1 root root 64 Mar 23 15:25 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:25 8 -> /tmp/run_info.log
=======================
(NV_CAM_NETVUE3_)
lrwx------ 1 root root 64 Mar 23 15:25 4 -> /tmp/run_info.log
lrwx------ 1 root root 64 Mar 23 15:25 8 -> /tmp/run_info.log
=======================
(tail)
lr-x------ 1 root root 64 Mar 23 15:26 3 -> /tmp/run_info.log
=======================
cat: can't open '/proc/3383/stat': No such file or directory
ls: /proc/3383/fd: No such file or directory
I.总结
在编程当中,尤其是Linux编程,对文件操作是非常重要的,很多人知道利用fopen、floce、fwrite、fread等C库接口对文件进行操作,
但遗憾的是多数程序员都对文件操作知之甚少。从而致使自己代码质量不高,健壮性比较差。出问题的时候,无法从根源去解决。
对于文件操作,我们需要熟知以下几点:
⑴ 熟练使用 fopen、fclose等API接口
⑵ 文件系统当中,一个文件对应内核一个inode结构,一个inode对应多个file结构,意味着一个文件可以被多个进程打开。
⑶ 查看某个文件被哪些进程打开。从而合理关闭文件句柄。
⑷ 如果文件被多个进程打开,但被暴力的删除了该文件,该文件空间一直存在,除非该文件被所有的进程释放。
⑸ 若某文件系统(如:ext2)某个文件被进程打开,那么是不能umount该文件系统的。
可能的提示信息:Device or resource busy.
错误代码:EBUSY (16)
⑹ 在多进程编程中,谨慎使用fork等系统接口,否则可能带来意想不到的bug。