log bash script output to file with timestamp
I'm trying to get this little script here to output to a logfile with a timestamp. I've got everything mostly working but the timestamp. Is there a better way to do this? it's my first attempt at writing a script.
Code:
#!/bin/bash |
I'd say that's what most people do to log from scripts.
However you are doing only >>$LOGFILE which means you're only sending standard output (STDOUT - a/k/a file descriptor 1) to the log. If there were errors it would go there. Most people redirect standard error (STDERR - a/k/a file descriptor 2) to the log as well. The shorthand to do this would be: >>$LOGFILE 2>&1 The 2 >&1 says to redirect file descriptor 2 to file descriptor 1. Note that order is important. Typing: 2>&1 >>$LOGFILE wouldn't work because STDERR would go to whatever STDOUT was defined as BEFORE you did the redirect of STDOUT. Another option is not to put the redirects in the script at all but rather to do them at invocation. For example if your script were called billybob.sh you could type: billybob.sh >>pathto/logfile 2>&1 This is commonly done if you're putting the script into cron: * * * * * /pathto/billybob.sh >>pathto/logfile 2>&1 It's really a matter of personal preference. Even in cron you can call a script that has its own internal logging defined. |
ahhhh. I had read that but it didn't make sense. Now it does. I'm still having a problem with the timestamp though. It prints exactly what I typed instead of the actual time and date.
i.e. Code:
date +%H:%M:%S : Starting work Code:
03262012 20:37.00 : starting work Code:
#!/bin/bash |
You missed the closing backticks in the command substitutions:
Code:
echo "`date +%H:%M:%S` : Starting work" >> $LOGFILE 2>&1 Code:
date "+%m%d%Y %H:%M:%S" Code:
date "+%m%d%Y %T" Code:
echo "$(date "+%m%d%Y %T") : Starting work" >> $LOGFILE 2>&1 |
Wow! that's awesome! I'll give it a try! thank you so so much! (yes I'm very excited I learned something cool and new)
|
$(..) is defined in the posix standard, which means it's supported by all modern bourne-based shells. So unless you have to script for really old shells, there's no reason at all to use backticks any more, and several reasons not to.
$(..) is highly recommended over `..` Frankly, the backticks have been deprecated for so long that I have to wonder just where and why they are still being learned by so many new scripters. Just who is it that keeps teaching them? |
Quote:
|
Yep - I've been doing shell scripting for many years but it is only in the last couple that I started using $() instead of back ticks - mainly because of 2 different presentations I saw at user group meetings about shell scripting in which both presenters mentioned it among other things they were suggesting.
The nesting is a major benefit to using $(). |
Quote:
|
In my case I've been doing *nix for many years, going back to before when linux became commonly/commercially available.
Back then we only had backticks & they still exist in many many scripts, especially non-bash on non-linux systems. :) Which one I use now tends to vary a bit, but I still tend towards them on non-linux ;) |
Except that even on non-Linux systems ksh or posix shell accepts the $(..) syntax for most UNIX flavors.
When I first started working on UNIX, Linux wasn't even a gleam in Linus' eye. :D The funny thing about UNIX is that it has been "going away any day now" for over 40 years but is still going strong. That reminds me of the Dilbert cartoon referenced in Wikipedia: Quote:
|
Quote:
http://linuxadvices.blogspot.ru/2013...-to-reuse.html Here is the code: Code:
write_log() |
All times are GMT -5. The time now is 05:02 AM. |