Managing Logs

Sometimes if the Linux installation is behaving oddly, it generate a heap of logs. This can quickly fill up your storage space. I encountered this issue when using Linux on android via linuxdeploy.

Here is the command for checking the log size in Linux. This command will list the top 10 log files based on its size (large to small). This is in Debian system.

sudo du -ah /var/log | sort -h -r | head -n 10

Output may look like this

2.1M    /var/log
512K    /var/log/nginx
508K    /var/log/auth.log.1
288K    /var/log/dpkg.log.1
240K    /var/log/nginx/access.log
136K    /var/log/letsencrypt
96K     /var/log/nginx/access.log.1
88K     /var/log/apt
72K     /var/log/auth.log.2.gz
56K     /var/log/bootstrap.log

You can always open and study the logs to understand the problems. Or you can delete them using this command. Here we are deleting nginx logs with the size 512K.

echo "" > /var/log/nginxbash

You can also create a script to automatically delete the log at certain interval, like a day or every 12 hours. Here is an example script file. You can modify the script based on the log files you want to clear.

Place the script file (logerase.sh) at /usr/local/bin.

Give execute permission to the script via this command

chmod +x /usr/local/bin/logerase.sh

Automate the script execution via Crontab

sudo crontab -e

Add the following line in crontab to execute the script daily at 00:00

0 0 * * * /usr/local/bin/logerase.sh

Last updated