[SOLVED] log bash script output to file with timestamp
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
#
LOGFILE=/home/test/test.log
echo "`date +%H:%M:%S : Starting work" >> $LOGFILE
#
service test stop >> $LOGFILE
#
mv /var/run/test/test.pid /var/run/test/test.pid.bak
#
echo "`date +%H:%M:%S : process file moved" >> $LOGFILE
#
service test start >> $LOGFILE
#
echo "`date +%H:%M:%S : Finished" >> $LOGFILE
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:
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
instead of
Code:
03262012 20:37.00 : starting work
------------------latest script-----------------
Code:
#!/bin/bash
#
LOGFILE=/home/test/test.log
echo "`date +%H:%M:%S : Starting work" >> $LOGFILE 2>&1
#
service test stop >> $LOGFILE 2>&1
#
mv /var/run/test/test.pid /var/run/test/test.pid.bak
#
echo "`date +%H:%M:%S : process file moved" >> $LOGFILE 2>&1
#
service test start >> $LOGFILE 2>&1
#
echo "`date +%H:%M:%S : Finished" >> $LOGFILE 2>&1
Last edited by daisychick; 03-27-2012 at 03:09 PM.
Reason: added latest update to script
Furthermore, if you want the calendar date, not only the time, you should have something like this:
Code:
date "+%m%d%Y %H:%M:%S"
or simply
Code:
date "+%m%d%Y %T"
An additional note: since you're using bash, why not try the new syntax for command substitution (unless you care for compatibility with the older Bourne Shell)? Example:
$(..) 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.
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?
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?
Unfortunately, even the Advanced Bash Scripting Guide starts the chapter on command substitution with backticks. The new syntax is relegated to the bottom of the chapter!
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.
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?
Google... You search, your read, you learn. Then you realize you broke something and come here. I don't know about most, but I try to research something before coming to the community and asking for help so I have a basic grasp of the concept. (Also because I'm the first to ask someone "did you read the FAQs??! Did you try google?")
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.
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:
The lamentation "You had ones? Lucky you, all we had were zeros!", commonly used in IT industry[original research?], also originated in a Dilbert comic strip. This dates from a strip from September 1992,[14] in which Dilbert responds by saying "You had zeros? We had to use the letter 'O'".
I'm going to have to dig out the Dilbert book that strip appears in since it seems Wikipedia doubts the research on it.
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
#
LOGFILE=/home/test/test.log
echo "`date +%H:%M:%S : Starting work" >> $LOGFILE
#
service test stop >> $LOGFILE
#
mv /var/run/test/test.pid /var/run/test/test.pid.bak
#
echo "`date +%H:%M:%S : process file moved" >> $LOGFILE
#
service test start >> $LOGFILE
#
echo "`date +%H:%M:%S : Finished" >> $LOGFILE
write_log()
{
while read text
do
LOGTIME=`date "+%Y-%m-%d %H:%M:%S"`
# If log file is not defined, just echo the output
if [ "$LOG_FILE" == "" ]; then
echo $LOGTIME": $text";
else
LOG=$LOG_FILE.`date +%Y%m%d`
touch $LOG
if [ ! -f $LOG ]; then echo "ERROR!! Cannot create log file $LOG. Exiting."; exit 1; fi
echo $LOGTIME": $text" | tee -a $LOG;
fi
done
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.