Having a single Log file for all the shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having a single Log file for all the shell scripts
# 1  
Old 12-04-2014
Having a single Log file for all the shell scripts

Bash on RHEL 6.2

We have multiple shell scripts executed by cron jobs. Each Shell script has its own log files currently. Most of them have logs generated using spool command from Oracle RDBMS's sqlplus commmand.

Sample:
Code:
    #!/bin/bash

    sqlplus -s scott/tiger << EOF

    spool /home/oracle/scripts/logs/script1.log

    Some SQL goes here;

    spool off

    quit
    EOF

Now, our log monitoring team wants to have just one log file where all the shell scripts will scripts write to. We'll call it the 'CentralLogFile.log' for this discussion.

They say it is easy for their log mining tools to search for keywords in one log file rather than multiple log files. They will rotate this CentrallogFile file on a monthly basis.

But, I do not want to stop the current way of shell scripts generating separate log files for each execution. I want the shell scripts to write to both the CentralLogFile and the dedicated log file.

I found a simple & Elegant solution by Corona in the following link

Enable logging from within the shell script

Code:
exec >logfile
exec 2>&1

So, is it OK if use the following commands in red lines in all of my scripts so that it will write to the central log as well ?
Do you see any drawbacks, issues with this approach ?

Code:
#!/bin/bash

exec >/home/oracle/logmaster/CentralLogFile.log 
exec 2>&1

sqlplus -s scott/tiger << EOF

spool /home/oracle/scripts/logs/script1.log

Some SQL goes here;

spool off

quit
EOF

# 2  
Old 12-04-2014
It will work, but it will overwrite the logfile every time. You might want to do exec >> logfile instead, so it appends.

Do you expect these scripts to produce output all the time, or are they supposed to be completely silent except for errors?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-04-2014
Thank You very much Corona.

Yes. All output must be captured to the central log. So, the following should work. Right ?

Code:
exec >> /home/oracle/logmaster/centrallog.txt
exec 2>&1

BTW...I don't need to use two greater than symbols for the second line as show below
Code:
exec 2>>&1

. Right ?
# 4  
Old 12-04-2014
Nope. Since you're not opening a file, just copying a stream, nothing's truncated.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-04-2014
Quote:
Originally Posted by kraljic
Bash on RHEL 6.2
. . .
But, I do not want to stop the current way of shell scripts generating separate log files for each execution. I want the shell scripts to write to both the CentralLogFile and the dedicated log file.
I don't think it will work.
1) the exec redirection will redirect stdout and stderr to some file, but the sql spool directive will open a new file without considering the redirection. It might work if you drop the spool.
2) With your exec you only open/redirect to one single file. There's nothing done to write dedicated AND central log files. The tee command provides a second etc. output stream, but I don't know how it could be used for this request.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-04-2014
Thank You corona.

Hi Rudic
I did a quick test with the following script. It writes to both script1.log and CentralLogFile.log . Need to test this in the actual scripts.

Code:
#!/bin/bash
exec >> /home/oracle/SCRIPTS/lgq/CentralLogFile.log
exec 2>&1
sqlplus -s "/ as sysdba" << EOF
spool /home/oracle/SCRIPTS/lgq/script1.log
alter session set nls_date_format='DD-MM-YYYY HH24:MI';
select sysdate from dual;
select sysdate+1 from dual;
select 'ajab' from dual;

spool off


quit
EOF

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log file is not updating when I run shell scripts scheduled thru crontab

Hi Forum, Good Day! I have created an empty html file wtih permissoin 777 created shell script(with permission 777) , code is below. #=======================start============== . /data09/oracle/apps_st/appl/D_oraapp095.env rm -rf /home/mnp/Test_log.txt echo... (1 Reply)
Discussion started by: kartheekbk
1 Replies

2. Shell Programming and Scripting

Combining two scripts into a single script

Hi Folks, I have two scripts that are used to start and stop services these scripts are at the location /opt/app/tre , so that start.sh internally starts the components and stop.sh internally stop all the components, now rite now if I have to stop the services then i need to go first the... (9 Replies)
Discussion started by: punpun66
9 Replies

3. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

4. UNIX Desktop Questions & Answers

creating an executable file from shell scripts

Hi Friends, I have a shell script which does some operations etc, would it be possible to create an executable file out from this shell script? meaning the executable file is not editable, thus the source code will not be visible to other users for copyright reasons. Please help, thanks! (1 Reply)
Discussion started by: kokoro
1 Replies

5. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies

6. Shell Programming and Scripting

shell scripts to grep dn in ldif file

Hi gurus out there, 1)I am using ksh, in solaris 10. 2)I have one ldif file, I need to output user DN with attributes=<some pattern> to a file. Example: dn: uid=joy,ou=People,o=abc.com,o=isp nswmExtendedUserPrefs: meAutoSign=true nswmExtendedUserPrefs: meSignature=Regards, Joy... (3 Replies)
Discussion started by: bulkbiz
3 Replies

7. Shell Programming and Scripting

how to log if the program contains both bourne-shell & pearl scripts

I have a program (say, MyProgram) written in Bourne-shell script, but at some point it calls another script written in pearl, as illustrated below: #!/bin/sh ..... case $x in 1) ConfigSystem1 ( b-shell script) 2) ConfigSystem2 ( pl) 3) ConfigSystem3 (b-shell) .... Then I create... (0 Replies)
Discussion started by: bluemoon1
0 Replies

8. Shell Programming and Scripting

FTP is using shell scripts create ? for file

ftp -n -v <<EOF verbose open 3.57.40.79 user infodvlp pr0gram ascii lcd /home/a501420038/GLA/Success_Load/ cd /ftp/SrcFiles/csg/InstruAsia/ get AU_Success_Log.txt close quit EOF Please help on this, this gives the out put "AU_Success_Log.txt?" As question mark in the last what will... (1 Reply)
Discussion started by: a501420038
1 Replies

9. Shell Programming and Scripting

shell scripts that parse log files

hi all ,i would like a shell script that parses log files and checks the contents for any anonalities,please help,thanks (4 Replies)
Discussion started by: trueman82
4 Replies

10. Shell Programming and Scripting

Plz Help To convert xml file to text file using bourn shell scripts

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> <country_code>AK></country_code>... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question