Enable logging from within the shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Enable logging from within the shell script
# 1  
Old 10-17-2014
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.

Code:
# cat myscript.sh
#### I actually want some logging mechanism here which will redirect the output to a log file
echo 'hello world'

# ./myscript.sh > /tmp/mylog.txt
#
# cat /tmp/mylog.txt
hello world

But, this script is not going to be executed manually or through cron job. Instead, it is invoked by Symantec Netbackup program remotely.
So, I don't get a chance to redirect the output to a log file as shown above.
So, I need to have some mechanism within the script itself to redirect the output to a file. Any idea how I can do this ?
# 2  
Old 10-17-2014
These two lines in your script will redirect stdout and stderr to a file.
Code:
exec >logfile
exec 2>&1

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-17-2014
THANK YOU.. THANK YOU...
YOU ARE AN ANGEL Corona. God Bless.

How does this work ? Man page of exec (some Bash Bulletin thing) wasn't very helpful to me.


The netbackup way is to litter the shell script with lot of echo >> as shown below.

Code:
RMAN_LOG_FILE=/usr/openv/netbackup/logs/rman/rman_DB_backup_weekly_${CUR_DATE}.out


if [ -f "$RMAN_LOG_FILE" ]
then
        rm -f "$RMAN_LOG_FILE"
fi


echo >> $RMAN_LOG_FILE
chmod 666 $RMAN_LOG_FILE


echo Script $0 >> $RMAN_LOG_FILE
echo ==== started on `date` ==== >> $RMAN_LOG_FILE
echo >> $RMAN_LOG_FILE
ORACLE_SID=BMIBPRD1
export ORACLE_SID


ORACLE_USER=oracle


TARGET_CONNECT_STR=/


RMAN=$ORACLE_HOME/bin/rman
echo >> $RMAN_LOG_FILE
echo   "RMAN: $RMAN" >> $RMAN_LOG_FILE
echo   "ORACLE_SID: $ORACLE_SID" >> $RMAN_LOG_FILE
echo   "ORACLE_USER: $ORACLE_USER" >> $RMAN_LOG_FILE
echo   "ORACLE_HOME: $ORACLE_HOME" >> $RMAN_LOG_FILE

This User Gave Thanks to John K For This Post:
# 4  
Old 10-17-2014
exec seems to do a lot of different things, but at core, all it means is "do (this) to the current process" -- instead of what a shell is usually used for, "do (this) to (that) new process".

So this is plain ordinary shell redirection, applied to the shell itself. In effect this allows it to open and close files. (Closing is a special syntax, like exec 2>&-) Files are referred to by arbitrary numbers. 0 is standard input, 1 is standard output, and 2 is standard error.

You could also run a command like exec commandname. Since this applies to the shell itself, the shell would be replaced by that command, and when it finished you'd have no more shell. This is what always happens, technically, but the shell normally makes a copy of itself first.

Last edited by Corona688; 10-17-2014 at 02:00 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use scl enable python command call with in bash shell script?

I have a bash shell script, within it i am using the below two commands . its failing right on scl enable command itself. if i run it by itself without a shell, it works fine. by default it is using pythin version 2.6 something. i want to use 3.4 version for that reason with in the shell... (3 Replies)
Discussion started by: cplusplus1
3 Replies

2. AIX

How could I enable logging of bind 9 forwarders messages?

How could I enable logging of bind 9 forwarders messages? I have the following but forwarded requests do not show up in the logs even when running named in debug 10 mode: logging { channel syslog_errors { // this channel will send errors or ... (0 Replies)
Discussion started by: Devyn
0 Replies

3. UNIX for Dummies Questions & Answers

How to enable syslog logging.

Hi, my question is probably quite easy. On one linux machine I have messages being constantly being written to /var/log/messages. An ntpd message comes in every few seconds. I can see new ones with tail messages. On the other machine there seems to be no messages arriving in /var/log/messages.... (3 Replies)
Discussion started by: jackiebaron
3 Replies

4. Shell Programming and Scripting

shell script for multiple logging

Hi All, I am preparing a script which executes following things: 1) Logs into 8 cluster one by one. 2) After logging into each cluster,it prints the cluster name & then exit from that cluster. 3) Then it logs to next cluster & peform the same task. Here is what i have written : for... (8 Replies)
Discussion started by: d8011
8 Replies

5. Solaris

How to enable logging in Solaris 10?

hi all, i want to log every thing happen in my server is any body can provide me with articals, pdf's is that correct to log every thing in the system what is the most important thing i should i log it (4 Replies)
Discussion started by: corvinusbsd
4 Replies

6. Shell Programming and Scripting

how to disable and enable <control>-c or -z in a shell script

Dear all, good day. i'm asking about how to disable <control>-c or <control>-z in the beginning of a shell script then enable it again before the script exit Best Regards ---------- Post updated at 04:41 AM ---------- Previous update was at 04:18 AM ---------- Dear All i found the... (3 Replies)
Discussion started by: islam.said
3 Replies

7. Shell Programming and Scripting

Shell Script for Logging into the Website

Hi ALL, Is there any way, to login into a website using Shell/Perl command/script? I am struggling on this from quite sometime but with no luck. Can you guys help, please? My sole purpose is to login a website (Which requires Username and Password) and then extract some information from... (3 Replies)
Discussion started by: parshant_bvcoe
3 Replies

8. Shell Programming and Scripting

Have a script running even with the shell logging out

Hi all, I wish to have a script running even if my session is disconnected. I've tried calling another session within it and using sudo to a different user, but it didn't work - as it was expected to do so :rolleyes: I guess I'll have to work with "nohup" command, right ? trying with the... (4 Replies)
Discussion started by: 435 Gavea
4 Replies

9. 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

10. UNIX for Dummies Questions & Answers

shell script error logging

Hi, I am writing a shell script (ksh, solaris 5.8). Script is X.sh variables declared: LOGFILE=X.log the script is removing a file: /usr/bin/rm "$DUMP_FILE1".Z >> $LOGFILE If I kick off the script from the command line as follows: ./X.sh > X.con 2<& 1 (1 Reply)
Discussion started by: niamh
1 Replies
Login or Register to Ask a Question