Logging Script!!!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logging Script!!!
# 1  
Old 02-24-2012
Question Logging Script!!!

I am trying to write a script that can be called by other scripts to print output to a log. I also want to be able to rotate the log file for this script with a max of 5 logs files. This is what I have so far:

Code:
#/bin/ksh
 
. /taipso75/scripts/IPSM_run_profile.sh*
log=${IPSM_HOME}/log
(
echo "----------------------------------------------------------------------"
echo "Log on" $0 "am" `date '+%Y-%m-%d %H:%M:%S'`
echo "------------------"
IPSM_LOG_DIR=${IPSM_HOME}/log
date=`date '+%Y-%m-%d'`
yesterday=`date --date='1 days ago' +%F`
cd ${IPSM_HOME}/log
ls -lt | grep ".txt" | cut -c 37-47 | sort -u | grep -vE "${date}|${yesterday}" | while read crea_date; do
        tar_files=`ls -ltr *.txt | grep " ${crea_date} " | cut -c 54-`
        tgz=${crea_date}.tgz
        echo tar cfz ${tgz} ${tar_files}
        tar cfz ${tgz} ${tar_files}
        ls -ltr *.txt | grep " ${crea_date} "
        ls -ltr *.txt | grep " ${crea_date} " | while read del_files; do
                del_file=`echo ${del_files} | awk '{print $NF}'`
                #echo ${del_file}
                rm   ${del_file}
        done
        echo "------------------"
done
# Delete the *.tgz-Files older than 30 days
while [ `ls -l *.tgz | wc -l` -gt 20 ]; do
        rm=`ls -r *.tgz | tail -1`
        echo ${rm} "have been deleted!"
        rm -rf ${rm}
done
) >> ${IPSM_HOME}


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 02-24-2012 at 11:17 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-24-2012
Are you logging just a small block now and then, or streaming to the log on a pipe? For the first, just put the date or time in the file name, and it grows a new file every hour, shift, day, week, month. For the latter, some side process needs to interrupt it using kill, to send it to trap, to start a new log with an entry pointing to the old log, if that works then close off writing the old log with an entry tying it to the new log and finally, delete any excess logs, without closing the input pipe. When writing blocks, if there are many logging processes, be sure to log whole blocks in one write so lines are not split or intermingled. Pile the message elements up in vm:
Code:
echo $( write-it-all ) >>$log_name

or an env var:
Code:
msg=x ; msg="$msg y" ;  msg="$msg z" ; echo "$msg" >>$log_name

and then echo it out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

2. Shell Programming and Scripting

Enable logging from within the shell script

Bash on Oracle Linux 6.3 I have a shell script whose output I want to redict to a log file. So, I can simply redirect the output as shown below. # cat myscript.sh #### I actually want some logging mechanism here which will redirect the output to a log file echo 'hello world' #... (3 Replies)
Discussion started by: John K
3 Replies

3. Shell Programming and Scripting

script - multiple telnets with logging

We had a system outage, and now I am trying to figure out how to get raw data to store in a log file. I have a flat file that has multiple IP port line. I want to telnet to each and log each. But as soon as I connect to the first, it stays there. This is on an HPUX 11.23 system. I dont think... (1 Reply)
Discussion started by: lhradowy
1 Replies

4. Emergency UNIX and Linux Support

Perl script logging via Shell script

Hello I wrote a nice Perl script that is intended to find and copy some files when getting a TERM signal. Now I wanted to make a shell script that starts/restarts the Perl script if it crashes/ends because of errors and make a log of all output the Perl Script gives. The problem is that it won't... (11 Replies)
Discussion started by: al0x
11 Replies

5. Shell Programming and Scripting

Perl script logging via Shell script

Hello I wrote a nice Perl script that is intended to find and copy some files when getting a TERM signal. Now I wanted to make a shell script that starts/restarts the Perl script if it crashes/ends because of errors and make a log of all output the Perl Script gives. The problem is that it won't... (1 Reply)
Discussion started by: al0x
1 Replies

6. Shell Programming and Scripting

logging into another server through script

Hello everybody, I have one small issue... :( When i'm trying to connect another unix box through below script.. #!/usr/bin/bash ssh $1 <<EOF Commands . . exit EOF But getting some syntax error "-sh: syntax error at line 2: `end of file' unexpected". I used to use... (2 Replies)
Discussion started by: raghu.iv85
2 Replies

7. Shell Programming and Scripting

Help with adding logging to a script

I'm pretty new to bash shell scripting and I was wondering how to add some logging to a script?:confused: (2 Replies)
Discussion started by: kp400sf
2 Replies

8. Shell Programming and Scripting

Can we keep our script alive while logging out.

Hi I want to keep my script running even when i am logged off from the box. can we run the script in background which can automatically run every hour? Please advise. Thank you (1 Reply)
Discussion started by: Prateek007
1 Replies

9. UNIX for Dummies Questions & Answers

logging as su from script

Hi, I am running a script where i need to run another command in a particular folder which I do not have access so I need to login as su to that folder and run that script...what are the options I need so that I can skip interactive mode ..here is what I tried.. #! /usr/bin/sh... (2 Replies)
Discussion started by: mgirinath
2 Replies

10. Shell Programming and Scripting

logging in Shell script

How to I write to a log file all the output that is displaying on the screen? with time stamp. thankx. (3 Replies)
Discussion started by: laila63
3 Replies
Login or Register to Ask a Question