Log File Maintenance and Cleanup
Log files sometimes take up a lot of disk space. Some applications have internal processes that run periodically to manage them and some do not. Often, this becomes a problems after a while as the logs consume all of your partition space.
You can manage these files yourself with a simple script running in a cronjob (or systemd timers if you’re so inclined) if they have a common naming convention and you have the proper access.
Let’s say you have an application called myapp that keeps
it’s logs in a directory called /opt/myapp/logs
and those
files all end with a .log
file extension.
cat >logmanage.sh <<"EOF"
#!/bin/sh
LOGDIR="/opt/myapp/logs"
# Compress all of the files older than a day
find ${LOGDIR} -name '*.log' -mtime +0 -exec compress {} \;
# Purge all of the logs older than a week
find ${LOGDIR} -name '*.Z' -mtime +7 -exec rm -f {} \;
EOF
These two commands will compress those files that are more than older than a day and remove the compressed files after a week.
Add a crontab entry to run this everyday and you’re all set.