Solved

Bash Logging

Posted on 2008-11-03
7
2,842 Views
Last Modified: 2013-12-26
I have a multi function bash script that needs to have good logging. I am currently using the following method, however is not exactly ideal.

function name() {
stuff here | tee -a ${LOG}
}

This works, however I would like to find a way to use one function to log all output from the script. I have also tried the following method which does not seem to work very well as it causes script errors.

function name() {
stuff here
}  | tee -a ${LOG}

The biggest thing with the logging function is that I must be able to see verbose information on the screen while it is also being captured to the log. I was able to get the following to do pretty decent logging, however it does not display anything on the screen.

function name() {
stuff here
}  >> ${LOG}

Any help would be greatly appreciated. Thanks again in advance.
0
Comment
Question by:Sam Cohen
7 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 200 total points
ID: 22867352
use the command script at the begining of the script

script filename
stuff here
exit

for more info, please see

man script
0
 
LVL 8

Author Comment

by:Sam Cohen
ID: 22867440
Ok, I added this to the start of the script, which is generating a timestamp, however thats all it does. Here is how it executes:

[root@centbox trunk]# ./webappinstaller.sh
Script started, file is scriptlog
[root@centbox trunk]#

Unfortunately the only problem is that this script requires user input and has a full blown menu. If the script just runs in the background it cannot be used.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22867492
if the script requires user input, how do you intend to run it as a background script?
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 8

Author Comment

by:Sam Cohen
ID: 22867534
I do not want it to run in the background. But when I use your suggestion (script) that is what happens. If I remove it the script works fine.
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 200 total points
ID: 22869571
It's trickier to do it from the script.

So long as you don't need to capture the user input, just do

./webappinstaller.sh | tee -a /some/log

If you don't want to type that at the command line, then just create a wrapper script to call the above line.
0
 
LVL 16

Expert Comment

by:ai_ja_nai
ID: 22916635
or you may redirect the script output to a log file. Then redirect the log to /dev/stdout
0
 
LVL 6

Assisted Solution

by:peter991
peter991 earned 100 total points
ID: 23537005
You could start a second shell/window and run "tail -f your_logfile"

You could also turn on debugmode by using

#!/usr/bin/bash -xv

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question