如何清理 lsof 中 deleted 状态的文件
在日常运维中,经常遇到磁盘空间满,但是找不到相应文件的情况。
通常这种情况都是文件被删除,但是还被进程占用,造成du与df结果不一致。
处理办法通常是停止占用文件的进程。
但是如果进程不能被停止呢?
另一个处理办法就是通过清空文件释放空间。
处理办法如下:
- 通过lsof | grep deleted 找到未能删除掉的文件,确定占用的进程号;
- 通过 ls -l /proc/PID/fd/* | grep 文件名,找到相应文件句柄;
- 清除文件内容 echo > /proc/PID/fd/FD_NUM
这个操作被不会将文件删除,而是通过将文档内容清空的方法释放空间,文件还是存在的。
实验如下:
1.创造一个大文件
使用dd创建1个5000MB的文件,看df的输出,可用空间从13G降到了7.5G。
[root@test1 /]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 30M 2.0G 2% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 39G 27G 13G 68% /
/dev/sda1 xfs 1.1G 394M 671M 37% /boot
tmpfs tmpfs 396M 0 396M 0% /run/user/0
[root@test1 /]# dd if=/dev/zero of=/delete.tmp bs=1000MB count=5
5+0 records in
5+0 records out
5000000000 bytes (5.0 GB) copied, 5.35441 s, 934 MB/s
[root@test1 /]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 30M 2.0G 2% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 39G 32G 7.5G 81% /
/dev/sda1 xfs 1.1G 394M 671M 37% /boot
tmpfs tmpfs 396M 0 396M 0% /run/user/0
[root@test1 /]# du -sh /delete.tmp
4.7G /delete.tmp
2.使用tail 打开文件
用tail 打开文件,保证删除文件时,文件仍被占用
[root@test1 /]# tail -f /delete.tmp
3.删除文件
使用rm 删除文件,在以下df输出中会发现,可用空间还是7.5G,没有变化,但是文件已经消失了。
[root@test1 /]# rm -f /delete.tmp
[root@test1 /]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 30M 2.0G 2% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 39G 32G 7.5G 81% /
/dev/sda1 xfs 1.1G 394M 671M 37% /boot
tmpfs tmpfs 396M 0 396M 0% /run/user/0
[root@test1 /]# du -sh /delete.tmp
du: cannot access ‘/delete.tmp’: No such file or directory
4.查找删除的文件
lsof 显示了deleted状态的文件名和大小(5000000000)。
[root@test1 ~]# lsof | grep deleted
tail 419 root 3r REG 253,0 5000000000 55981 /delete.tmp (deleted)
5.查找文件句柄
[root@test1 ~]# ll /proc/419/fd | grep delete.tmp
lr-x------ 1 root root 64 May 23 16:05 3 -> /delete.tmp (deleted)
6.清空文件
[root@test1 ~]# echo > /proc/419/fd/3
[root@test1 ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 30M 2.0G 2% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 39G 27G 13G 68% /
/dev/sda1 xfs 1.1G 394M 671M 37% /boot
tmpfs tmpfs 396M 0 396M 0% /run/user/0
那么/proc/PID/fd 是啥呢?
man proc
/proc/[pid]/fd/
This is a subdirectory containing one entry for each filewhich the process has open, named by its file descriptor,and which is a symbolic link to the actual file. Thus, 0is standard input, 1 standard output, 2 standard error,and so on.For file descriptors for pipes and sockets, the entrieswill be symbolic links whose content is the file type withthe inode. A readlink(2) call on this file returns astring in the format:type:[inode]For example, socket:[2248868] will be a socket and itsinode is 2248868. For sockets, that inode can be used tofind more information in one of the files under/proc/net/.For file descriptors that have no corresponding inode(e.g., file descriptors produced by bpf(2),epoll_create(2), eventfd(2), inotify_init(2),perf_event_open(2), signalfd(2), timerfd_create(2), anduserfaultfd(2)), the entry will be a symbolic link withcontents of the formanon_inode:<file-type>In many cases (but not all), the file-type is surroundedby square brackets.For example, an epoll file descriptor will have a symboliclink whose content is the string anon_inode:[eventpoll].In a multithreaded process, the contents of this directoryare not available if the main thread has alreadyterminated (typically by calling pthread_exit(3)).Programs that take a filename as a command-line argument,but don't take input from standard input if no argument issupplied, and programs that write to a file named as acommand-line argument, but don't send their output tostandard output if no argument is supplied, cannevertheless be made to use standard input or standardoutput by using /proc/[pid]/fd files as command-linearguments. For example, assuming that -i is the flagdesignating an input file and -o is the flag designatingan output file:$ foobar -i /proc/self/fd/0 -o /proc/self/fd/1 ...and you have a working filter./proc/self/fd/N is approximately the same as /dev/fd/N insome UNIX and UNIX-like systems. Most Linux MAKEDEVscripts symbolically link /dev/fd to /proc/self/fd, infact.Most systems provide symbolic links /dev/stdin,/dev/stdout, and /dev/stderr, which respectively link tothe files 0, 1, and 2 in /proc/self/fd. Thus the examplecommand above could be written as:$ foobar -i /dev/stdin -o /dev/stdout ...Permission to dereference or read (readlink(2)) thesymbolic links in this directory is governed by a ptraceaccess mode PTRACE_MODE_READ_FSCREDS check; see ptrace(2).Note that for file descriptors referring to inodes (pipesand sockets, see above), those inodes still havepermission bits and ownership information distinct fromthose of the /proc/[pid]/fd entry, and that the owner maydiffer from the user and group IDs of the process. Anunprivileged process may lack permissions to open them, asin this example:$ echo test | sudo -u nobody cattest$ echo test | sudo -u nobody cat /proc/self/fd/0cat: /proc/self/fd/0: Permission deniedFile descriptor 0 refers to the pipe created by the shelland owned by that shell's user, which is not nobody, socat does not have permission to create a new filedescriptor to read from that inode, even though it canstill read from its existing file descriptor 0.
如何清理 lsof 中 deleted 状态的文件相关推荐
- 清理iOS中的“其他”空间垃圾文件
关于如何清理 iOS 里的"其他"空间的教程,网上搜索那是一大堆,不过都是对于2010年某坛某篇"技术文"的无数次简单复制粘帖,可行性已经被各路尝试者们踩到了地 ...
- linux恢复deleted状态的文件,Linux恢复被删除的文件 How To Recover Deleted Files From Your Linux System ....
先介绍一些文件的基本概念, 文件实际上是一个指向inode的链接, inode链接包含了文件的所有属性, 比如权限和所有者, 数据块地址(文件存储在磁盘的这些数据块中). 当你删除(rm)一个文件, ...
- 利用posix_fadvise清理系统中的文件缓存
利用posix_fadvise清理系统中的文件缓存 leoncom c/c++,unix2011-08-03 当我们需要对某段读写文件并进行处理的程序进行性能测试时,文件会被系统cache住从而影响I ...
- linux中的echo%3e文件,Linux文件已删除,引用未释放(deleted)
问题描述: 警告文件磁盘已经满了,通过:df -h, du -sh * 等指令配合使用,发现当前系统任然存在大量可以使用的空间.大量剩余的磁盘空间不清楚怎么丢失了- 问题重现: 通过指令:lsof | ...
- linux系统空间不足,lsof看到异常的delete状态的文件。
linux系统空间不足,lsof看到异常的delete状态的文件. 参考文章: (1)linux系统空间不足,lsof看到异常的delete状态的文件. (2)https://www.cnblogs. ...
- 如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片...
在我们迭代项目的过程中,经常会启用某些功能,或者修改某些界面的问题,那么问题来了,这样很容易出现大量的冗余.java文件,冗余资源文件,一些冗余的界面文件等.那么问题既然出现了,那么如何去解决呢,这就 ...
- Flink 1.8.0中的状态生存时间特性:如何自动清理应用程序的状态
对于许多状态流式计算程序来说,一个常见的需求是自动清理应用程序的状态(state),以便有效地控制状态大小,或者控制程序访问状态的有效时间(例如受限于诸如GDPR等法律条规).Apache Flink ...
- python 打印文件名_在Python中打印文件名,关闭状态和文件模式
python 打印文件名 Prerequisite: Opening, closing a file/open(), close() functions in Python 先决条件: 在Python ...
- SAP中状态参数文件最高状态和最低状态的理解
在SAP中订单的应用还是比较广泛的.如下图所示:生产订单,内部订单,质量订单,成本收集器等. 而这些类型的订单在系统后台的定义界面都基本相似.这说明虽然订单的类型有不同,但其结构和应用原理基本相似:针 ...
最新文章
- 秀秀博客大赛50强的礼物
- git在项目中的实际运用
- HDU Problem - 1533 Going Home(费用流板子题)
- JavaScript专题之模拟实现new
- c语言键盘控制数码管显示,3*4矩阵键盘控制4位数码管显示的C程序
- 编辑了基因,就能成为人生赢家?5本书给你答案
- mysql sql 一部分记录_MySQL性能优化实践(很全面,值得收藏)
- java在文本域内添加按钮_JAVA中在窗体中添加了一个文本框,然后再文本框中输入字符,如何...
- python有什么用途-Python是什么 Python的用处
- C++11标准后的C++阅读书目
- vue数据大屏使用数字字体
- 练习一: 提示:emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno所属部门编号) dept部门
- K8S集群中网络通信故障常见问题的排查思路
- Python项目实战:爬取糗事百科最热门的内涵搞笑段子
- java团队管理_团队管理的“五大核心要素”
- 汇编语言与高级语言的区别
- Egg Config
- c语言教程——简单易懂
- C# Excel版本兼容性问题
- 数字电路基础知识系列(一)之LC滤波器基础知识