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/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... |
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 |
list="a b c"
for a in $list; do echo $a; done should work :) |
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 |
This works using bash shell:
Code:
#!/bin/bash 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. |
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 |
Quote:
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 This is untested and will probably require some tweeking on your part. |
All times are GMT -5. The time now is 02:19 AM. |