Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question
1  
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 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 at 4:08

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.