LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian
User Name
Password
Debian This forum is for the discussion of Debian Linux.

Notices


Reply
  Search this Thread
Old 10-21-2005, 03:34 AM   #1
sridhar11
Member
 
Registered: Dec 2002
Location: us
Posts: 108
Blog Entries: 360

Rep: Reputation: 15
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
 
Old 10-21-2005, 04:40 AM   #2
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
!/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...

Last edited by /bin/bash; 10-21-2005 at 04:46 AM.
 
Old 10-21-2005, 05:01 AM   #3
sridhar11
Member
 
Registered: Dec 2002
Location: us
Posts: 108

Original Poster
Blog Entries: 360

Rep: Reputation: 15
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
 
Old 10-21-2005, 05:26 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
list="a b c"
for a in $list; do echo $a; done

should work

Last edited by nx5000; 10-21-2005 at 05:29 AM.
 
Old 10-22-2005, 02:48 AM   #5
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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
 
Old 10-22-2005, 03:28 AM   #6
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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.


Last edited by /bin/bash; 10-23-2005 at 04:38 AM.
 
Old 10-23-2005, 02:17 PM   #7
sridhar11
Member
 
Registered: Dec 2002
Location: us
Posts: 108

Original Poster
Blog Entries: 360

Rep: Reputation: 15
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
 
Old 11-04-2005, 04:11 AM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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.

Last edited by /bin/bash; 11-04-2005 at 04:26 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script adding autostart gnome script Coolrunr Programming 3 01-01-2009 02:23 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Debian

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration