1833401 Members
3474 Online
110052 Solutions
New Discussion

Exit Status

 
SOLVED
Go to solution
hpuxrox
Respected Contributor

Exit Status

Ok, I have the following scripts to rotate logs off to another directory for auditing,

Begin Cut ------------------

#! /bin/sh

DATE=`date +%m%d%y`

/usr/bin/cp -p /var/adm/syslog/syslog.log /home/audit/logs/syslog.log.$DATE
/usr/bin/cp -p /var/adm/syslog/syslog.log /var/adm/syslog/syslog.log.yesterday

/usr/bin/cp -p /var/adm/syslog/mail.log /home/audit/logs/mail.log.$DATE
/usr/bin/cp -p /var/adm/syslog/mail.log /var/adm/syslog/mail.log.yesterday

/usr/bin/cp -p /var/adm/sulog /home/audit/logs/sulog.$DATE
/usr/bin/cp -p /var/adm/sulog /var/adm/sulog.yesterday

/usr/bin/cp -p /var/mail/root /home/audit/logs/root_mail.$DATE


if [ $? = 0 ]
then

/usr/bin/cat /dev/null > /var/adm/syslog/syslog.log
/usr/bin/cat /dev/null > /var/adm/syslog/mail.log
/usr/bin/cat /dev/null > /var/adm/sulog
/usr/bin/cat /dev/null > /var/mail/root

/usr/bin/chown -R root:audit /home/audit/logs
/usr/bin/chmod -R 660 /home/audit/logs

fi

End Cut -------------------


What I am looking for a a cleaner way to check after each copy, and if any of the copies fail, exit the script. "I dont want to zero my logs if I have a problem writing to my externel storage"

Currently, I believe, my script is only checking the last copy for status. Any ideas?


4 REPLIES 4
Geoff Wild
Honored Contributor

Re: Exit Status

This doesn't really help your script, but have you looked at logrotate?

http://hpux.ee.ualberta.ca/hppd/hpux/Sysadmin/logrotate-2.5/

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Mark Grant
Honored Contributor
Solution

Re: Exit Status

Correct, it is only checking the last cp.

How abut some thing like this

cp -p /var/adm/syslog/syslog.log /home/audit/logs/syslog.log.$DATE && cat /dev/null > /var/adm/syslog/syslog.log

The && means the stuff on the right of it only will be executed if the stuff on the left succeeded.

On the other hand, if you were a little less particular about where you were copying to, something like this might be better

for i in adm/syslog/syslog.log /adm/syslog/mail.log etc etc
do
cp /var/$i /var/$i.$DATE && > /var/$i
done

Never preceed any demonstration with anything more predictive than "watch this"
Michael Schulte zur Sur
Honored Contributor

Re: Exit Status

Hi

yes,only the last exit code is checked.

Place the copy inside a function, which does the copy and gets two parameter.

mycp()
{
/usr/bin/cp -p ${1} ${2}
if [ $? = 0 ]
then
/usr/bin/cp /dev/null ${1}
fi
}

call for example:
mycp /var/adm/syslog/syslog.log /home/audit/logs/syslog.log.$DATE


Michael
Jeroen Peereboom
Honored Contributor

Re: Exit Status

L.S.

If I remember correctly you may run into problems using 'cat /dev/null > logfile'
if the logfile is still open.
I always use '>logfile' to empty logfiles, especially my syslog.log

JP.