How to recover deleted Apache Log

Scenario: The server is running Apache and by mistake one of the log files gets deleted. How, we can recover it?

Currently, Apache server is working perfectly:

Login/Switch to as root user:

Move to the httpd in log directory:

cd /var/log/httpd

Delete the log file:

rm -f access_log

So the log file has been deleted,now find the process number for main apache procees that owned by root:

ps aux | grep httpd

In this case, the pid for the main apache process is 1784.

Now lets list the file descriptors:

ls -lsa /proc/1784/fd

Stop the apache service:

service httpd stop

Now copy the access log file that is marked as deleted (In this case it is 7) into the log directory:

cp /proc/1784/fd/7 /var/log/httpd/access_log

Start the apache service again:

service httpd start

Access the webpage again from your server:

Verify the log file:

tail -f /var/log/httpd/access_log

It’s working :-)

Hope this will help you!