简体   繁体   English

恢复卡在Linux shell进程中的已删除文件

[英]Recover Deleted File Stuck In Linux shell process

I have a background process that is running for a long time and using a file to write the logs in it. 我有一个运行了很长时间的后台进程,并使用文件在其中写入日志。 It`s size has increased too large. 它的大小增加了太大。 I just deleted the file and created a new one with the same name and same permission and ownership but the new file does not get any entry. 我只是删除了该文件,并使用相同的名称,相同的权限和所有权创建了一个新文件,但是新文件未获得任何条目。 Old file is marked as deleted and still being used by the process which can clearly be seen by lsof command. lsof命令可以清楚地看到旧文件被标记为已删除,并且仍在使用中。

Plz let me know, is there any way that I can recover that file and. 请让我知道,有什么方法可以恢复该文件呢?

Your positive response will really be much helpful. 您的积极回应确实会很有帮助。

If the file is still open by some process, you can recover it using the /proc filesystem. 如果文件仍通过某些进程打开,则可以使用/proc文件系统恢复该文件。

First, check the file descriptor number under which that file is opened in that process. 首先,检查在该过程中用来打开该文件的文件描述符号。 If the file is opened in a process with PID X , use the lsof command as follows: 如果在使用PID X的进程中打开文件,请使用lsof命令,如下所示:

lsof -p X

This will show a list of files that are currently opened by X . 这将显示X当前打开的文件列表。 The 4th column shows the file descriptors and the last column shows the name of the mount point and file system where the file lives (ignore the u , r and other flags after the file descriptor number, they just indicate whether the file is opened for reading, writing, etc.) 第四列显示文件描述符,最后一列显示文件所在的安装点和文件系统的名称(忽略文件描述符编号之后的ur和其他标志,它们仅指示是否打开文件以供读取,写作等)

If the file descriptor number is Y , you can access its contents in /proc/X/fd/Y . 如果文件描述符号为Y ,则可以在/proc/X/fd/Y访问其内容。 So, something like this would recover it: 因此,像这样的东西可以恢复它:

cp /proc/X/fd/Y /tmp/recovered_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM