首页 > 系统相关 >Linux不杀进程清理 lsof中deleted 状态的文件

Linux不杀进程清理 lsof中deleted 状态的文件

时间:2022-10-23 18:02:06浏览次数:57  
标签:deleted lsof dev standard inode fd file Linux proc

一、问题

之前已经写过一篇 "<<Linux磁盘占用率与使用率不符>>"
将进程kill掉就行

但是如果进程不能被停止呢
之前碰到一个例子:
朋友的系统盘空间爆满,du -sh查找不出问题,lsof|grep delete查出来是/tmp目录下一堆mysql进程的删除文件占着空间
ps了一下进程号,发现正好是mysql运行的进程,按照上面办法重启mysql肯定能解决问题,但是工作时间数据库跑着业务肯定没法重启
不重启又会因为系统盘爆满,mysql没指定临时目录(数据目录有单独磁盘,tmpdir没指定默认用/tmp目录),无法写到/tmp目录下,影响业务

网上搜了一下临时解决办法,通过清空文件释放空间。
处理办法如下:
通过lsof | grep deleted 找到未能删除掉的文件,确定占用的进程号;
通过 ls -l /proc/PID/fd/* | grep 文件名,找到相应文件句柄;
"比如:lr-x------ 1 root root 64 May 23 16:05 3 -> /delete.tmp (deleted)"
清除文件内容 echo > /proc/PID/fd/FD_NUM #此处FD_NUM为上一步查出来的文件句柄,通常为一个数字

这个操作被不会将文件删除,而是通过将文档内容清空的方法释放空间,文件还是存在的。
如果此文件还在往里面写数据,还是会重新膨胀起来占据空间
这只是一个临时解决办法,最终还是要通过重启释放

二、附录/proc/PID/fd

那么/proc/PID/fd 是啥呢?
man proc
/proc/[pid]/fd/

This is a subdirectory containing one entry for each file
which the process has open, named by its file descriptor,
and which is a symbolic link to the actual file. Thus, 0
is standard input, 1 standard output, 2 standard error,
and so on.

For file descriptors for pipes and sockets, the entries
will be symbolic links whose content is the file type with
the inode. A readlink(2) call on this file returns a
string in the format:

type:[inode]

For example, socket:[2248868] will be a socket and its
inode is 2248868. For sockets, that inode can be used to
find 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), and
userfaultfd(2)), the entry will be a symbolic link with
contents of the form

anon_inode:<file-type>

In many cases (but not all), the file-type is surrounded
by square brackets.

For example, an epoll file descriptor will have a symbolic
link whose content is the string anon_inode:[eventpoll].

In a multithreaded process, the contents of this directory
are not available if the main thread has already
terminated (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 is
supplied, and programs that write to a file named as a
command-line argument, but don't send their output to
standard output if no argument is supplied, can
nevertheless be made to use standard input or standard
output by using /proc/[pid]/fd files as command-line
arguments. For example, assuming that -i is the flag
designating an input file and -o is the flag designating
an 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 in
some UNIX and UNIX-like systems. Most Linux MAKEDEV
scripts symbolically link /dev/fd to /proc/self/fd, in
fact.

Most systems provide symbolic links /dev/stdin,
/dev/stdout, and /dev/stderr, which respectively link to
the files 0, 1, and 2 in /proc/self/fd. Thus the example
command above could be written as:

$ foobar -i /dev/stdin -o /dev/stdout ...

Permission to dereference or read (readlink(2)) the
symbolic links in this directory is governed by a ptrace
access mode PTRACE_MODE_READ_FSCREDS check; see ptrace(2).

Note that for file descriptors referring to inodes (pipes
and sockets, see above), those inodes still have
permission bits and ownership information distinct from
those of the /proc/[pid]/fd entry, and that the owner may
differ from the user and group IDs of the process. An
unprivileged process may lack permissions to open them, as
in this example:

$ echo test | sudo -u nobody cat
test
$ echo test | sudo -u nobody cat /proc/self/fd/0
cat: /proc/self/fd/0: Permission denied

File descriptor 0 refers to the pipe created by the shell
and owned by that shell's user, which is not nobody, so
cat does not have permission to create a new file
descriptor to read from that inode, even though it can
still read from its existing file descriptor 0.

标签:deleted,lsof,dev,standard,inode,fd,file,Linux,proc
From: https://blog.51cto.com/u_13236892/5787402

相关文章

  • llinux下mysql建库、新建用户、用户授权、修改用户密码
    1、创建新的数据库1.1、root用户登录mysqlmysql-uroot-p 1.2、查看现有数据库showdatabases; 1.3、新建数据库,此命名为cjccreatedatabasecjc; 1.4、删......
  • Linux NFS服务
    NFS:  NetworkFileSystem网络文件系统,是一个文件系统,不是一种协议类型。使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的文件。说明:  NFS是内核提供的......
  • Linux下驱动开发_块设备驱动开发(硬件上采用SD卡+SPI协议)
    一、前言块设备主要为存储设备设计的框架。在前面章节​里介绍了块设备驱动编写思路,并且利用内存模拟了硬件存储,完成了块设备驱动开发测试。这一篇文章将采用SD卡作为存储......
  • Linux下IIC驱动编写,介绍IIC子系统框架的使用
    一、IIC协议介绍说起IIC,搞单片机,嵌入式的那肯定是接触的比较多的。串口、IIC、SPI这3个协议在单片机阶段应该是用比较多的,很多的外设模块,芯片都是串口、IIC、SPI等协议与主......
  • Linux 时钟子系统
    一文搞懂|Linux时钟子系统Clock时钟就是SoC中的脉搏,由它来控制各个部件按各自的节奏跳动。比如,CPU主频设置,串口的波特率设置,I2S的采样率设置,I2C的速率设置等等。这......
  • Linux - 文件查看命令(干货)
    格式:文件查看命令  文件名1.cat:由第一行开始看文件内容,读文章,读代码等等;2.tac:从最后一行开始看,和cat是反着的3.nl:显示的时候,顺便输出行号(我们在看代码的时候,希望显......
  • LINUX第五章 定时器及时钟服务
    第五章定时器及时钟服务硬件定时器定时器是由时钟源和可编程计数器组成的硬件设备。时钟源通常是一个晶体振荡器,会产生周期性电信号,以精确的频率驱动计数器。使用一个......
  • linux 中 top 命令使用方法
    环境说明Centos7系统top命令是最直观最好用的查看节点负载的命令。可以实时动态的刷新服务器状态信息,并且支持交互命令自定义显示内容,功能算是很强大了。在终端中输入to......
  • Oracle Linux 7u2 启动错误 XFS_WANT_CORRUPTED_GOTO
    OracleLinux7u2 (OracleLinux-R7-U2-Server-x86_64-dvd.iso) 安装海锋五笔(ibus-table-chinese-wubi-haifeng-1.4.6-3.el7.noarch.rpm)后,启动系统失败。 XFS: In......
  • Linux-文件的基本权限
    文件权限由十个字母组成第一个字母:d:文件的目录l:文件的连接(linkfile)-:代表是一个文件剩下的九个字母,三个为一组,rwx:分别代表可读,可写,可执行;没有这个权限用-代替操......