Logrotate.d for every httpd instance for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logrotate.d for every httpd instance for loop?
# 8  
Old 03-08-2016
The following makes sense (to me)
Code:
for instance in /apps/apache/*httpd*
do
  if [ -d "$instance" ] && [ ! -f /etc/logrotate.d/"$instance" ] && service "$instance" status >/dev/null 2>&1
  then
    sed "s|examplehost|$instance|g" </usr/local/scripts/examplelogconfig >/etc/logrotate.d/"$instance"
  fi
done

I don't know what the /usr/local/scripts/stage/ is for; you can add a cp command in the loop block if needed.
# 9  
Old 03-09-2016
Code:
ls -d /apps/apache/*httpd* | while read -r name
do	if [ ! -f "/apps/apache/$name" ]
	then	cp /usr/local/scripts/examplescript.txt /usr/local/scripts/stage/ \
        sed 's|examplehost|$name|g' /usr/local/scripts/stage/$name > "/usr/local/scripts/stage/$name"
	fi
done

I tried to change what you started not quite working for me.

---------- Post updated at 01:06 PM ---------- Previous update was at 01:04 PM ----------

Ok one min I didn't see this last post....

---------- Post updated at 01:09 PM ---------- Previous update was at 01:06 PM ----------

I think this should work

Code:
for instance in /apps/apache/*httpd*
do
  if [ -d "$instance" ] && [ ! -f /etc/logrotate.d/"$instance" ] && service "$instance" status >/dev/null 2>&1
  then
    sed "s|examplehost|$instance|g" </usr/local/scripts/examplelogconfig >/usr/local/scripts/stage/"$instance"
  fi
done

I have to copy them into /usr/local/scripts/stage/ before I can later move them to /etc/logrotate.d/ during non production hours etc.

---------- Post updated at 01:19 PM ---------- Previous update was at 01:09 PM ----------

My output is this from running that....
Code:
>I removed /dev/null 2>&1

/apps/apache/httpd-cibcbeta: unrecognized service
/apps/apache/httpd-cibctrain: unrecognized service
/apps/apache/httpd-fundsdev: unrecognized service
/apps/apache/httpd-fundstrain: unrecognized service
/apps/apache/httpd-mlidev: unrecognized service
/apps/apache/httpd-mliqat: unrecognized service
/apps/apache/httpd-mlitrain: unrecognized service
/apps/apache/httpd-mliuat: unrecognized service

---------- Post updated at 01:30 PM ---------- Previous update was at 01:19 PM ----------

and if I remove
Code:
&& service "$instance" status >/dev/null 2>&1

Then I get...

Code:
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-cibcbeta: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-cibctrain: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-fakedir: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-fundsdev: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-fundstrain: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-mlidev: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-mliqat: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-mlitrain: No such file or directory
./logrot.sh: line 8: /usr/local/scripts/stage//apps/apache/httpd-mliuat: No such file or directory

# 10  
Old 03-09-2016
My bad, $instance needs to be the short file name.
Code:
if cd /apps/apache
then
  for instance in *httpd*
  do
    if [ -d "$instance" ] && [ ! -f /etc/logrotate.d/"$instance" ] && service "$instance" status >/dev/null 2>&1
    then
      sed "s|examplehost|$instance|g" </usr/local/scripts/examplelogconfig >/usr/local/scripts/stage/"$instance"
    fi
  done
fi

BTW the staging only makes sense for testing, i.e. if there is no test system.
I would rather let the script directly copy to /etc/logrotate.d/, and run the script at the desired maintenance slot.

Last edited by MadeInGermany; 03-09-2016 at 07:05 PM..
# 11  
Old 03-16-2016
I'm still getting

when I removed /dev/null 2>&1

I get several of these errors

/apps/apache/httpd-fakedir: unrecognized service
# 12  
Old 03-16-2016
In my last script there is
Code:
  for instance in *httpd*

So $instance becomes the short name httpd-fakedir not the pathname /apps/apache/httpd-fakedir
This User Gave Thanks to MadeInGermany For This Post:
# 13  
Old 03-16-2016
I understand that but it's not copying the file over to the directory and then doing the sed statement it just gives me the unrecognized
# 14  
Old 03-16-2016
Can you just have 1 log rotate that does them all?

Code:
/apps/apache/*/logs/*log {
    rotate 7
    compress
    missingok
    notifempty
    sharedscripts
    daily
    delaycompress
    nomail
    postrotate
          SERV=${1#*apache/}
          SERV=${SERV%%/*}
          /sbin/service $SERV reload > /dev/null 2>/dev/null || true
    endscript
}

Edit: I can see sharedscripts would cause an issue here (would only reload the last SERVER rotated)
if you don't mind restarting everything after it's done you could just use /usr/bin/killall -HUP httpd in the postrotate:


Code:
/apps/apache/*/logs/*log {
    rotate 7
    compress
    missingok
    notifempty
    sharedscripts
    daily
    delaycompress
    nomail
    postrotate
        /usr/bin/killall -HUP httpd
    endscript
}


Last edited by Chubler_XL; 03-16-2016 at 08:30 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop to run commands - after the previous instance completed

Hi All, I am trying to call a shell script in a loop. I want my first instance to complete, and then the 2nd instance of the command to start - and so on. eg. I am running this script 30 times. The wrapper script needs business date, from_time,to_time & server_name as inputs. script_name... (2 Replies)
Discussion started by: neil.k
2 Replies

2. UNIX for Dummies Questions & Answers

Logrotate configuration

Hi all, i would like to have a configuration on log rotate that will gzip my log files with date pattern %Y-%m-%d move these files to an olddir, but i want to avoid that the logrotate removes my files from old dir newer than 180 days i applyied this config #logrotate config to compress files... (3 Replies)
Discussion started by: charli1
3 Replies

3. AIX

Logrotate - /etc/logrotate.conf does't exist

Hi Admins. I have installed logrotate rpm on Aix 6.1. After the installation of rpm, I don't find /etc/logrotate.conf file and /etc/logrotate.d dir . The config file is located in /opt/freeware/etc/logrotate.conf. When I ran logrotate -v /opt/freeware/etc/logrotate.conf I get below... (2 Replies)
Discussion started by: snchaudhari2
2 Replies

4. Shell Programming and Scripting

Logrotate - I am not able to rotate files using logrotate

I have written script which is working in Home directory perfectly and also compressing log files and rotating correctly. But, when i try to run script for /var/log/ i am able to get compressed log files but not able to get rotation of compressed log files. Please suggest. I am using below command... (5 Replies)
Discussion started by: VSom007
5 Replies

5. Red Hat

Logrotate on RHEL 5

Hi All, Can any one let me me know if i run /usr/sbin/logrotate /etc/logrotate.conf will it read the whole /var/log/messages or it just see the time stamp of message file & then rotate the logs. Just wants to know is logrotation based on messages file contents or messages file time stamp. ... (1 Reply)
Discussion started by: ajaincv
1 Replies

6. Linux

LogRotate

Hi I am trying to rotate specific log by using logrotate function in unix I have made following entry in the /etc/logrotate.conf file /var/log/testlog/debug_log { daily rotate 7 create compress } But only issue is that my other logs like /var/log/messages,... (2 Replies)
Discussion started by: SiddhV
2 Replies

7. Red Hat

logrotate httpd logfiles

Hi, I need to logrotate logs in directories in /var/log/httpd/. There are 4 directories in /var/log/httpd/... these directories are /var/log/httpd/access/ /var/log/httpd/debug/ /var/log/httpd/error/ /var/log/httpd/required/ Each of the access, required, error and debug directories have around... (1 Reply)
Discussion started by: renuka
1 Replies

8. UNIX for Advanced & Expert Users

logrotate with /etc/logrotate.conf file

Hi there, I want to rotate the logfiles which are located in /var/log/jboss/tomcat* so I have created a file named as 'tomat' in /etc/logrotate.d/tomcat with the following content. # cat /etc/logrotate.d/tomcat /var/log/jboss/tomcat_access_log*.log { daily nocreate ... (2 Replies)
Discussion started by: skmdu
2 Replies

9. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi, PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime. I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file. However, I'd like to make sure... (2 Replies)
Discussion started by: jjshell
2 Replies

10. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies
Login or Register to Ask a Question

Featured Tech Videos