Solved

Bash Logging

Posted on 2008-11-03
7
2,810 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now