handle file by month

Hi,

I want to write a shell script which handle an oracle alert log so that when it changes month, the script generates a new file with month in the filename.

Thanks in advance.

Leim

What do you mean by "when it changes month" ?

Do you just want to create a new file, say, "alert_Mon.log" on the 1st of every month, where Mon is the 3-character month abbreviation ?

if [ "$(date +%d)" == "01" ] ; then  touch alert_$(date +%b).log; fi

tyler_durden

Thanks for reply.

The scenario is like:

  1. A cron job run alert_mgt.sh every 5 mins
  2. If there is no error in the alert.log, it merges the alert.log to this month alert log say, alert.log.Sept2009.
  3. If there is error in the alert.log, it grep it and mail it out to dba, then it merges the alert.log to alert.log.Sept2009. (At the same time, it can write some note in the place where the error ocurred in the alert.log.Sept2009)
  4. When month changes (10/01/2009 00:00:00), it generate a new log alert.log.Oct2009.

Thanks in advance.
leim

You can look at logrotate if your distribution supports.

logrotate(8) - Linux man page

ok Here is the script... You need to adjust according to you system some paths etc ...
Most of it will work once you make necessary changes.

------------------------------------------------------------------------------

ERRMESG="[ $(/bin/date) ]Something went wrong with Monitoring script.."
if grep -i Error /pathto/log/alert.log
then
ERRMESG="[ $(/bin/date) ] Errors in the logs $(grep -i Error /pathto/log/alert.log)"
echo $ERRMESG | mail -s 'Error in /pathto/log/alert.log' dba@whatever
else
ERRMESG="[ $(/bin/date) ]: The log file has been truncated."
fi
cat /pathto/log/alert.log >> "/pathto/log/alert.log.$(/bin/date +%b%Y)"
echo $ERRMESG >> "/pathto/log/alert.log.$(/bin/date +%b%Y)"

------------------------------------------------------------------------------

Do reply back with changes and working script ...