Recover Deleted Nginx Logs on Linux Using lsof – Step‑by‑Step Guide
This tutorial demonstrates how to verify a running Nginx process, deliberately delete its access log, and then use the lsof command together with the /proc filesystem to locate the deleted file descriptor and restore the log data on a Linux system.
This article records a practice of using lsof to recover mistakenly deleted Linux files, specifically an Nginx access log.
Prerequisites
Ensure the Nginx process is running.
Delete the log file with rm -f /var/log/nginx/access.log.
Use lsof to help restore the log data.
Check Nginx status
[root@master10 ~]# systemctl status nginxView the (now missing) log file
[root@master10 ~]# tail /var/log/nginx/access.logSimulate accidental deletion
[root@master10 ~]# rm -f /var/log/nginx/access.log
[root@master10 ~]# tail /var/log/nginx/access.log
tail: cannot open ‘/var/log/nginx/access.log’ for reading: No such file or directoryRecover the log using lsof
1. Use lsof to find processes holding the deleted file:
[root@master10 ~]# lsof | grep /var/log/nginx/access.log
nginx 1439 root 5w REG 253,0 1524 17117944 /var/log/nginx/access.log (deleted)
nginx 1440 nginx 5w REG 253,0 1524 17117944 /var/log/nginx/access.log (deleted)
nginx 1441 nginx 5w REG 253,0 1524 17117944 /var/log/nginx/access.log (deleted)The main Nginx process (PID 1439) still holds the file descriptor, so the file can be recovered.
Locate the file descriptor in /proc
2. Navigate to the process's file‑descriptor directory:
[root@master10 fd]# cd /proc/1439/fd
[root@master10 fd]# llAmong the listed symbolic links, entry 5 points to /var/log/nginx/access.log (deleted).
Read and restore the deleted file
3. Display the contents of the descriptor and write them back to the original path:
[root@master10 fd]# cat 5 > /var/log/nginx/access.log
[root@master10 fd]# cat /var/log/nginx/access.logThe log file is now restored.
Original source: https://www.cnblogs.com/funlyp/p/18351218 (copyright belongs to the original author).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
