Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Logrotate.d for every httpd instance for loop?

Tags
shell script, shell scripts, solved

Login to reply

 
Thread Tools Search this Thread
# 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 05: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
The Following User Says Thank You to MadeInGermany For This Useful Post:
xgringo (03-16-2016)
# 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 06:30 PM..
Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Loop to run commands - after the previous instance completed neil.k Shell Programming and Scripting 2 03-06-2018 10:22 AM
Logrotate - /etc/logrotate.conf does't exist snchaudhari2 AIX 2 06-21-2013 01:27 PM
Logrotate - I am not able to rotate files using logrotate VSom007 Shell Programming and Scripting 5 04-15-2013 02:23 PM
Logrotate on RHEL 5 ajaincv Red Hat 1 09-05-2011 02:26 AM
LogRotate SiddhV Linux 2 02-10-2010 01:30 PM
logrotate httpd logfiles renuka Red Hat 1 01-11-2010 10:25 AM
logrotate with /etc/logrotate.conf file skmdu UNIX for Advanced & Expert Users 2 10-17-2009 07:04 AM
[PHP] endless loop mimics a cron. Make sure only one instance is running jjshell Shell Programming and Scripting 2 12-24-2008 11:00 AM
logrotate bug? jjinno UNIX for Advanced & Expert Users 0 06-09-2008 04:24 PM
replace first instance(not first instance in line) IronHorse7 Shell Programming and Scripting 3 02-07-2008 12:29 PM


All times are GMT -4. The time now is 05:33 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password