Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


# 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

5 1 * * * /usr/local/jboss/server/default/

and add cron with crontab /root/

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
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 '14 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 '14 at 4:08
logrotate ftw... – dmourati Jul 19 '15 at 19:18

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


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.