Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2919
  • Last Modified:

Bash Logging

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
Sam Cohen
Asked:
Sam Cohen
3 Solutions
 
omarfaridCommented:
use the command script at the begining of the script

script filename
stuff here
exit

for more info, please see

man script
0
 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
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
 
omarfaridCommented:
if the script requires user input, how do you intend to run it as a background script?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Sam CohenConsultant to Digital/DeveloperAuthor Commented:
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
 
TintinCommented:
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
 
ai_ja_naiCommented:
or you may redirect the script output to a log file. Then redirect the log to /dev/stdout
0
 
peter991Commented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now