LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   logrotate shell script help with existing script (https://www.linuxquestions.org/questions/debian-26/logrotate-shell-script-help-with-existing-script-375331/)

sridhar11 10-21-2005 03:34 AM

logrotate shell script help with existing script
 
Hi,

I have a shell script what it does is

we need keep all clients monthly logs to /var/log/apache2/logs folder
Under this folder we have different clients directories.Each directory
should contain the logfiles that is access.log and error.log files for
each folder.Every month end logrotate should run and it should keep the
log files in monthnameandyear folder with monthnameandyear.tgz file.



Example:-

logfiles location /var/log/apache2/logs under this folder every client is having their folder for logs
in side that apache log files are there i.e error.log,access.log.

Every month logrotate should run and it should create a folder with monthnameandyear under this monthname folder

that perticular month logfiles .tgz file shouls be there.

ex:- /var/log/apache2/logs/bahu/may2005/may2005.log.tgz

script as follows and when i try to run this i am getting the following error

Error

script: line 1: !/bin/sh: No such file or directory
script: line 4: syntax error near unexpected token `('
script: line 4: `set LIST = (user1 user2 user3 user4)'


Script as follows

!/bin/sh

set noclobber
set HOMEDIR = /var/log/apache2/logs
set LIST = (user1 user2 user3 user4)
set MONLIST = (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
set MONTH = `date +%m`
set YR = `date +%Y`

@ PREMON = $MONTH - 1

# Check if the previous month is December
if ($PREMON == 0) then
@ PREMON = 12
endif

# set month in name
set MONTH = $MONLIST[$PREMON]

foreach DIR ($LIST)

# set to a home directory
cd $HOMEDIR/$DIR

# creating dir
if (! -d $MONTH$YR) then
mkdir $MONTH$YR
endif

#egrep "$MONTH" access.log | egrep "$YR" > $MONTH$YR.log
#egrep "$MONTH" error.log | egrep "$YR" > error_$MONTH$YR.log

test -f access.log && mv -f access.log $MONTH$YR.log
test -f error.log && mv -f error.log error_$MONTH$YR.log

test -f access.log || cp -f /dev/null access.log
test -f error.log || cp -f /dev/null error.log

# compressing file
tar zcvf $MONTH$YR.log.tgz $MONTH$YR.log
mv -f $MONTH$YR.log.tgz $MONTH$YR/

# removing file
rm -f $MONTH$YR.log
rm -f error_$MONTH$YR.log

end

kill -HUP `cat /var/log/apache2/logs/httpd.pid`
~

Plese help me where i am going wrong


Thanks in advance

/bin/bash 10-21-2005 04:40 AM

!/bin/sh
should be
#!/bin/sh

set LIST = (user1 user2 user3 user4)
set MONLIST = (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
Try adding space before and after parenthesis.
set LIST = ( user1 user2 user3 user4 )
set MONLIST = ( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )

<edit> Also /bin/sh is probably just a symlink to a shell, so you should make sure it is pointing to the proper shell, e.g. /bin/bash or /bin/ksh etc...

sridhar11 10-21-2005 05:01 AM

thanks mate i have changed as you suggested presently i have below in my script

#!/bin/bash
set noclobber
set HOMEDIR = /var/log/apache2/logs
set LIST = ( user1 user2 user3 user4 )
set MONLIST = ( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )


but still i am getting the following error

script: line 4: syntax error near unexpected token `('
script: line 4: `set LIST = ( user1 user2 user3 user4 )'


i am not getting clue where it is going wrong

Waiting for your help to fix this

nx5000 10-21-2005 05:26 AM

list="a b c"
for a in $list; do echo $a; done

should work :)

/bin/bash 10-22-2005 02:48 AM

Probably you have some extra whitespace in that command.
Try this:
set LIST=( user1 user2 user3 user4 )
set MONLIST=( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )


What shell are you using?
This doesn't look like any bash commands.
@ PREMON = $MONTH - 1
@ PREMON = 12

/bin/bash 10-22-2005 03:28 AM

This works using bash shell:
Code:

#!/bin/bash

LOGDIR=/var/log/apache2/logs
LIST=(user1 user2 user3 user4)

# Get monthyear in name
MONTHYR=`date +%b%Y`

for APACHEUSER in ${LIST[*]}
  do
  cd "$LOGDIR/$APACHEUSER"
  # creating dir
  [ ! -d $MONTHYR ] && mkdir $MONTHYR

  test -f access.log && mv -f access.log $MONTHYR.log
  test -f error.log && mv -f error.log error_$MONTHYR.log

  test -f access.log || cp -f /dev/null access.log
  test -f error.log || cp -f /dev/null error.log

  # compressing file
  tar zcvf $MONTHYR/$MONTHYR.log.tgz *$MONTHYR.log

  # removing file
  rm -f *$MONTHYR.log

done

kill -HUP `cat /var/log/apache2/logs/httpd.pid`


I am wondering why you are writing a script to do this when the program logrotate will do the work for you.

man logrotate

<edit> kill -HUP ... s/b after done, otherwise you'll reset apache everytime you compress/store a user directory.


sridhar11 10-23-2005 02:17 PM

thanks for your script this script no need of setting the month list correct.I have checked the logrotate to do thid after check this i thought it is not possible with logrotate.If you have the some idea how to use logrotate for this that would be great.

Waiting for your valuble help

/bin/bash 11-04-2005 04:11 AM

Quote:

If you have the some idea how to use logrotate for this that would be great.
postrotate/endscript
The lines between postrotate and endscript (both of which must
appear on lines by themselves) are executed after the log file
is rotated. These directives may only appear inside of a log
file definition. See prerotate as well.


prerotate/endscript
The lines between prerotate and endscript (both of which must
appear on lines by themselves) are executed before the log file
is rotated and only if the log will actually be rotated. These
directives may only appear inside of a log file definition. See
postrotate as well.


firstaction/endscript
The lines between firstaction and endscript (both of which must
appear on lines by themselves) are executed once before all log
files that match the wildcarded pattern are rotated, before pre-
rotate script is run and only if at least one log will actually
be rotated. These directives may only appear inside of a log
file definition. See lastaction as well.


lastaction/endscript
The lines between lastaction and endscript (both of which must
appear on lines by themselves) are executed once after all log
files that match the wildcarded pattern are rotated, after
postrotate script is run and only if at least one log is
rotated. These directives may only appear inside of a log file
definition. See lastaction as well.


Using these four directives allows you to write some pretty complex scripts which will be executed by logrotate.

You could have something like:
Code:

# sample logrotate configuration file
/var/log/apache2/logs/*/access.log /var/log/apache2/logs/*/error.log {
rotate 5
monthly
compress
prerotate
  LOGDIR=/var/log/apache2/logs
  LIST=(user1 user2 user3 user4)

  # Get monthyear in name
  MONTHYR=`date +%b%Y`

  for APACHEUSER in ${LIST[*]}
    do
    cd "$LOGDIR/$APACHEUSER"
    # creating dir
    [ ! -d $MONTHYR ] && mkdir $MONTHYR

    test -f access.log && cp  access.log $MONTHYR.log
    test -f error.log && cp error.log error_$MONTHYR.log

    # compressing file
    tar zcvf $MONTHYR/$MONTHYR.log.tgz *$MONTHYR.log

    # removing file
    rm -f *$MONTHYR.log
  done
endscript

postrotate
  kill -HUP `cat /var/log/apache2/logs/httpd.pid`
endscript
}

NOTE: You no longer need to delete the old files as logrotate will do that for you and will also keep 5 months of backups.

This is untested and will probably require some tweeking on your part.


All times are GMT -5. The time now is 02:19 AM.