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 |
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. |
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. |
Quote:
|
Or you could write a logging function and put it in a "bash library" file that all the other scripts could source.
|
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. |
Quote:
|
Quote:
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* |
Quote:
Code:
source /path/to/my_bash_library |
Quote:
|
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".
|
Quote:
I am really lost here. I do appreciate the help. I will do some research this weekend. Thanks |
Quote:
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} |
All times are GMT -5. The time now is 06:24 PM. |