1827439 Members
5481 Online
109965 Solutions
New Discussion

log file script

 
SOLVED
Go to solution
sanjit
Occasional Advisor

log file script

Hello everyone,

Can someone please assist me with writing a log script. I'm very new to Unix and defintely new to writing scripts. I need to be able to run a script that I can run from any directory, that will simply take my current log file, rename it to .(date). and then clean out the original file so it can be used the next day. My big thing here is for the script to be able to run from any directory and be able to pick up any type of lig files. Any help will be greatly appreciated here. Is someone has a sample script that I can use would also be very helpful. Thanks and have a nice day.
8 REPLIES 8
Robin Wakefield
Honored Contributor

Re: log file script

Hi,

The following processes all the log files defined in syslog.conf - sorry if it's heavy going, but will give you an idea:

======================================

#!/usr/bin/sh
#
# Set IFS to
IFS='
'
export IFS
PATH=/sbin:/usr/sbin:/usr/bin
export PATH
SHELL=/usr/bin/sh
export SHELL

CONFIG="${CONFIG:-/etc/syslog.conf}"
SYSLOGPID="${SYSLOGPID:-$(< /etc/syslog.pid)}"

#echo CONFIG is $CONFIG
#echo SYSLOGPID is $SYSLOGPID

today=$(date +%a)

typeset -i i=0 file=0

# Now get paths of all syslog files.
grep -v -e '^[ ]*#' -e '^[ ]*$' "${CONFIG}" |
while read selector action ; do
if [[ -f "${action}" ]] ; then
files[file]="${action}"
let file=file+1
fi
done

if [[ file -gt 0 ]] ; then
let i=0
while [[ i -lt file ]] ; do
rm -f "${files[i]}.${today}" "${files[i]}.${today}.Z"
if mv "${files[i]}" "${files[i]}.${today}" ; then
files[i]="${files[i]}.${today}"
else
files[i]=""
fi
let i=i+1
done
kill -1 $SYSLOGPID
let i=0
while [[ i -lt file ]] ; do
if [[ "" != "${files[i]}" ]] ; then
compress "${files[i]}"
fi
let i=i+1
done
fi

exit 0

=======================================

Rgds, Robin

Holger Knoppik
Valued Contributor

Re: log file script

Hi ! For cutomized scripts you maybe want to use a special directory. I usually put those scripts into /sbin/scripts.
To fill a log from your script do:
&1 >>
For creating logfiles with timestamps use the following within your script: (man date ;-)
----snip-------
#!/bin/sh
TIMESTAMP=`date +`
cp ${LOGFILE} ${LOGFILE}.${TIMESTAMP}
> ${LOGFILE}
# set logfile
# copy old logfile to logfile with timestamp
# and clear the old logfile to fill it with new
# informations.
----snip-------
To run a script form any position do the following: (You may add this entry to your .profile)
----snip-------
PATH=$PATH:/sbin/scripts
----snip-------
Never put a . into PATH, it is a potential security breach.

NOTE: For handling logfiles, the tool logrotate has been ported to HP-UX.
(See http://hpux.cs.utah.edu)
Hope this helps a bit
RGDS, Holger
Live long and prosper!
sanjit
Occasional Advisor

Re: log file script

Thanks for the response but both scripts are a little too complicated. Does anyone have a simpler scripts that will do what I asking for. Once again, I just need something that I can run from any directory, rename any logfile to *.(date), null the logfile. Hope this makes a little sense. Le tme know if anyone needs more info. Any help will be greatly appreciated.
linuxfan
Honored Contributor

Re: log file script

Hi Sanjit,

This script doesn't do much error checking

/Begin/

#!/bin/ksh

if [[ $# < 1 ]]
then
echo "Usage: $0 logfile"
fi

TIMESTAMP=`date +%m%d%y`
echo "Copying $1 to $1.${TIMESTAMP}"
cp $1 $1.${TIMESTAMP}
echo "/dev/null $1.${TIMESTAMP}"
cat /dev/null > $1

/End/

what kind of log files are you trying to move, there are already good scripts available which do lots of things, the above script will just
copy the logfile to logfile.monthdayyear format (eg logfile.092001)

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
sanjit
Occasional Advisor

Re: log file script

Hi Ramesh,
I'm trying to do this to any logfiles on any of my 30 systems that I have. I wonna be able to run this script from any directories also. Thanks for your help.
linuxfan
Honored Contributor
Solution

Re: log file script

Hi Sanjit,

As long as you give the right permissions to this script and give the right logfile (complete path to the logfile) you should be able to run this script from anywhere on any logfile (provided you have the right permissions on the logfile).

Like i said, if you are planning to do this for syslog, then there are better scripts like

1. logrotate.sh
http://www.introcomp.co.uk/examples/logrotate.html

2. rotatelog program by shaun Rowland (in Perl)
http://www.interhack.net/projects/rotatelog
(pretty nice)

3. Rotate logs
http://www.ginini.com.au/tools/rotatelogs/

Does this answer your question?

-Regards
Ramesh

They think they know but don't. At least I know I don't know - Socrates
Sachin Patel
Honored Contributor

Re: log file script

Hi Sanjit,
Lets take a look. You have 30 systems and do you want to run script on any systems? It will be pain after couple days. If you are able to remsh from one system to all other system then setup your script in one server.
In order to run from any directory you have to setup path in your .cshrc. Then you copy script in to that directory
#echo $PATH and choose appropriate directory where you can save your script.
Now lets take script from Ramesh and change little

#!/bin/ksh

if [[ $# < 1 ]]
then
echo "Usage: $0 logfile"
fi
TIMESTAMP=`date +%m%d%y`

for SYSTEM in server1 server2 server3?? ; do
echo "Copying $1 to $1.${TIMESTAMP}"
remsh ${SYSTEM} cp $1 $1.${TIMESTAMP}
remsh ${SYSTEM} echo "/dev/null $1.${TIMESTAMP}"
remsh ${SYSTEM} cat /dev/null > $1
done

/End/

If you have that script as cleanup then run
#cleanup /var/adm/syslog/mail.log
It will run on three server.

Sachin
Is photography a hobby or another way to spend $
sanjit
Occasional Advisor

Re: log file script

Thank you all very much for all the help. You guys are wonderful and this helps us people new to Unix learn a lot from all this.