Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Bash Logging

Posted on 2008-11-03
7
2,830 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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