LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2012, 02:15 PM   #1
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Rep: Reputation: Disabled
Question Shell Script


Ok. I am fairly new to Linux and have even less experience writing shell scripts. I was recently promoted to a position in IT and am being trained to get familiar with the system. What I am trying to do is write a script that other scripts can call and tell it to write a log file for that script. I also need it to be able to tell when the current log is full and for it to create a new log file. I will have a total of 5 log files at one time. When the current one gets full it will be replaced by the new one and the last one will fall out. Any help in getting me started will be greatly appreciated.

Thanks
 
Old 02-23-2012, 02:42 PM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
I am no scripting guru, but I think you need to clarify some points.

1) What I am trying to do is write a script that other scripts can call and tell it to write a log file for that script.

When a script runs it can create a log file by itself. It doesnt need to call another script to do this. It can be something as simple as redirecting output to a file. Read more about redirection here:
http://tldp.org/LDP/abs/html/io-redirection.html

2) I also need it to be able to tell when the current log is full and for it to create a new log file.

What is full? A log file is never really full unless of course you fill your disk up. A log file can just keep growing. I think what your suggesting is log rotation. When a log reaches a certain size (say 250MB) then you can create another log file.

Some daemons have this built in, you can tell then to rotate the log when it reaches a certain size. Scripts are different from daemons because the usually run once or are set in cron to run multiple times per day, they do no continuously run.

You can check the size of the file at the beginning of the script, if it is over a certain size you can rotate it. Something like this:

#!/bin/bash
DATE = `date +%m-%d-%y`
LOGSIZE=`du -h /path/to/log`
if [ $LOGSIZE > 2048000 ]; then
mv /path/to/log /path/to/log.$DATE
touch /path/to/log
fi

I hope that gets you started. If your new to Linux your better off learning the basics before learning scripting.
 
Old 02-23-2012, 02:47 PM   #3
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Savona,

Thank you. The last part is what I was refering too. I do need to rotate the log files. The other part I am trying to do is have a script that will write output from another script to a log file when it is called upon to do so without having to do it manually. I do know most of the basic but, alot of the functions still confuse me.
 
Old 02-23-2012, 03:15 PM   #4
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
Quote:
Originally Posted by Dougj75 View Post
Savona,

Thank you. The last part is what I was refering too. I do need to rotate the log files. The other part I am trying to do is have a script that will write output from another script to a log file when it is called upon to do so without having to do it manually. I do know most of the basic but, alot of the functions still confuse me.
Its hard to help you unless I know the script. Is it something you can post?
 
Old 02-23-2012, 09:22 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Or you could write a logging function and put it in a "bash library" file that all the other scripts could source.
 
Old 02-24-2012, 12:13 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,356

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
For log rotation, use the default tool http://linux.die.net/man/8/logrotate which you should find in /etc

logrotate.conf - basic conf

logrotate.d - dir with separate cfg for each logfile to rotate.
 
Old 02-24-2012, 07:41 AM   #7
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
Or you could write a logging function and put it in a "bash library" file that all the other scripts could source.
Ok. How would I go about doing this?
 
Old 02-24-2012, 07:45 AM   #8
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Dougj75 View Post
Ok. How would I go about doing this?
This is the directory and the scripts it contains.

taipso75@taipso75:/taipso75/scripts/IPSM >
IPSM_DB_LINKS.sh* IPSM_KM_TCP_daily.sh* IPSM_PROC_CHECK_daily.sh* IPSM_DB_PERFORMANCE_daily.sh* IPSM_KM_TCP.sh* IPSM_PROC_CHECK.sh* IPSM_DB_PERFORMANCE.sh* IPSM_KM_VT_daily.sh* IPSM_PROC_DUPLICATE_daily.sh* IPSM_DB_PING_daily.sh* IPSM_KM_VT.sh* IPSM_PROC_DUPLICATE.sh*
IPSM_DB_PING.sh* IPSM_NETWORK_PING_auto_gen.sh* IPSM_PROC_OLD.sh*
IPSM_DB_SELECT_daily.sh* IPSM_NETWORK_PING_daily.sh* IPSM_SERVERHEALTH_CPU.sh*
IPSM_DB_INVALIDOBJ_daily.sh* IPSM_DB_SELECT.sh* IPSM_NETWORK_PING.sh* IPSM_SERVERHEALTH_daily.sh*
IPSM_DB_INVALIDOBJ.sh* IPSM_DB_TIME_daily.sh* IPSM_PAI_PRINTER_daily.sh* IPSM_SERVERHEALTH_DISKSPACE.sh*
IPSM_DB_JOBS_daily.sh* IPSM_DB_TIME.sh* IPSM_PAI_PRINTER.sh* IPSM_SERVERHEALTH_MEM.sh*
IPSM_DB_JOBS.sh* IPSM_KM_SPOOLS_daily.sh* IPSM_PAI_TELEGRAM_daily.sh* IPSM_WEBSERVER_PING_daily.sh*
IPSM_DB_LINKS_daily.sh* IPSM_KM_SPOOLS.sh* IPSM_PAI_TELEGRAM.sh* IPSM_WEBSERVER_PING.sh*
 
Old 02-24-2012, 07:55 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Dougj75 View Post
Ok. How would I go about doing this?
In each script that you want to use common functions, insert something like this:
Code:
source /path/to/my_bash_library
The source command reads whatever is in my_bash_library, exactly as if it were in the calling script at that point.
 
Old 02-24-2012, 07:57 AM   #10
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
In each script that you want to use common functions, insert something like this:
Code:
source /path/to/my_bash_library
The source command reads whatever is in my_bash_library, exactly as if it were in the calling script at that point.
So I need multiple sacripts to do this?
 
Old 02-24-2012, 08:01 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Yes. Instead of "write a script that other scripts can call and tell it to write a log file", you would put a function in a bash library that would "write a log file" and source it from the "other scripts".
 
Old 02-24-2012, 08:03 AM   #12
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by catkin View Post
Yes. Instead of "write a script that other scripts can call and tell it to write a log file", you would put a function in a bash library that would "write a log file" and source it from the "other scripts".

I am really lost here. I do appreciate the help. I will do some research this weekend. Thanks
 
Old 02-24-2012, 10:08 AM   #13
Dougj75
LQ Newbie
 
Registered: Feb 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Dougj75 View Post
I am really lost here. I do appreciate the help. I will do some research this weekend. Thanks
Ok after some research and browsing through current script on my system this is what I have. Please review and give me some input!!!

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:

#/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}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 06:27 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM


All times are GMT -5. The time now is 05:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration