How to Delete Log Files in Linux
How to Delete Log Files in Linux / Empty a Log File in Linux or Unix
Scenario: When your webserver or any application log file size is quite huge or large?
You may try to archive and delete the log file by using
rm filename
But still, you might observe disk place is full even after deleting log files?
Why is space not being freed from disk after deleting a file in Red Hat Enterprise Linux?
Workaround:
Graceful shutdown of relevant process
First, obtain a list of deleted files which are still held open by applications:
$ /usr/sbin/lsof | grep deleted
deleted 23913 mohan 3r REG 1,4 331944 1152921500312070633 /System/Library/MessageTracer/SubmitDiagInfo.default.domains.searchtree
The lsof output shows the process with pid 23913 has kept file /System/Library/MessageTracer/SubmitDiagInfo.default.domains.searchtree open with file descriptor (fd) number 3
After a file has been identified, free the file used space by shutting down the affected process. If a graceful shutdown does not work, then issue the kill command to forcefully stop it by referencing the PID
Truncate File Size
$ echo > /proc/pid/fd/fd_number
For example, from the lsof output above:
file /proc/23913/fd/3
/proc/23913/fd/3: broken symbolic link to `/System/Library/MessageTracer/SubmitDiagInfo.default.domains.searchtree (deleted)'
The same reason will cause different disk usage from du command and df command
Resolution
But even after truncate you will get this issue frequently, so we can handle?
On Linux or Unix systems, deleting a file via rm or through a file manager application will unlink the file from the file system’s directory structure; however, if the file is still open (in use by a running process) it will still be accessible to this process and will continue to occupy space on the disk. Therefore such processes may need to be restarted before that file’s space will be cleared up on the filesystem.
And worried about how do I delete a log file in Linux without disturbing the running application?
Is there a proper way to clear log files on Unix?
The answer is Yes!!!!
Here is the sample script which we can leverage to truncate / empty log files without impacting the application.
if [ $(df -h / | tail -n 1 | awk -F ' ' '{print $5}' | awk '0+$1 >= 80 {print}' | wc -l) == 1 ] then cat /dev/null > /var/log/nginx/access.log cat /dev/null > /var/log/nginx/error.log cat /dev/null > /var/log/nginx/access-dm.log cat /dev/null > /var/log/nginx/access-aod.log-* cat /dev/null > /var/log/nginx/access-aod.log cat /dev/null > /var/log/nginx/access-mddb.log fi
Note: Script can be better, the above script is just a sample.
The above script will cover the below criteria:
a) Validate volume Used% if it more then 80% in my case volume is /
b) Truncate mentioned logs.
You can set this a crontab schedule to run every 5 min or as per your requirement, save the above script as .sh file e.g.; logtruncate.sh
*/5 * * * * /root/logtruncate.sh
References:
Happy Mentoring!