0

I need to compress and remove log files after some time from serwer. I have never write scripts before. So far i have script logs.sh:

#!/bin/bash
LOGS=/usr/local/jboss/server/default/log/
INFOLOG="server.log"
ERRORLOG="error.log"
BOOTLOG="boot.log"

# gzip files last modify at least 7 days ago, and not files that are identified above and that have already been compressed
find $LOGS -mtime +7 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" -not -name '*.gz' -exec gzip "{}" \;

# remove all logs older than 90 days
find $LOGS -mtime +90 -not -name "$INFOLOG" -not -name "$ERRORLOG" -not -name "$BOOTLOG" -print0 | xargs -0 -I xxx rm xxx 

and i'm using cron to do it evry 5 minutes after 1 AM from jboss-logs.sh

5 1 * * * /usr/local/jboss/server/default/logs.sh

and add cron with crontab /root/jboss-logs.sh

Is it correct? will it compress files older than 7 days and remove files older than 90? Will it works? It is safe - especially removing ?

3
  • 2
    I'd suggest you try it out by replacing the actual commands that touch files (i.e. gzip and rm) with something that prints out something like "Would gzip file xxxx.yy" and "Would rm file zzzz.ii". That way you'd see from the log cron outputs (or email if you configure it that way) what files it would touch at that moment and could validate if that is correct.
    – zagrimsan
    Feb 26, 2014 at 3:58
  • I've done exactly the same on Windows to archive logs. As long as the logs are saved a file per day basis (DailyRollingFileAppender) or more often (RollingFileAppender), the concept should work just fine (unless someone/something alters the mtime to be older, and that is likely not going to happen inadvertedly).However, why would you want to run the script every five minutes? It would be enough to run it once a day, or at max a couple of times a day unless you get massive amounts of logs per day.
    – zagrimsan
    Feb 26, 2014 at 4:08
  • logrotate ftw...
    – dmourati
    Jul 19, 2015 at 19:18

2 Answers 2

1

Use logrotate to rotate logs.

In /etc/logrotate.d/ you will find some already installed rotate patterns.

You basically define a pattern for files to handle on a daily base.

Logrotate is being triggered by cron.daily.

Use man logrotate to learn more about the easy, line based syntax.

1
  • To expound, logrotate handles compression, rotation and retention - exactly what you're trying to do.
    – thinice
    Mar 23, 2017 at 15:33
0

newsyslog.conf contains exactly what you need.

newsyslog periodically check files and rotate/compress/delete files. Also newsyslog can send signals to the processes that initiate reopening of logfiles.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .