LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2010, 08:19 PM   #1
digity
Member
 
Registered: Apr 2005
Posts: 105

Rep: Reputation: 15
adding logging to shell scripts


I have some simple shell scripts that perform backups and I was wondering how do I add logging? More specifically writing to a log file if any part of the script fails.

An example of what my shell scripts look like:

Code:
#!/bin/sh
cd /backups/web
timestamp=$(date +%Y%m%d%H%M%S)
tar -zvcf backup.web.$timestamp.tar.gz /var/www
chown digity:marines *.gz
chmod 660 *.gz
TIA
 
Old 01-01-2010, 08:52 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
You can write to the system log:

Code:
chown digity:marines *.gz 2>&1 | logger
will put anything usually output on stdout en stderr to /var/log/messages.

You can use logger -s to echo the output on stdout for debugging.

Use logger -t $0 to precede log entries with the command line you used to call you script.

However, for a command producing a lot of output like
Code:
tar -zvcf backup.web.$timestamp.tar.gz /var/www
you could better use
Code:
tar -zvcf backup.web.$timestamp.tar.gz /var/www > /var/log/myback.log
This has the advantage that you know which files were backed up. I find that invaluable.

jlinkels
 
Old 01-03-2010, 09:49 AM   #3
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
If you are doing multiple tars in a script, or other stuff you want logged, you should probably append it to your log file using ">>" rather than ">" so that you can get it all. You can also use `echo "some message $withvalues" >> $mylogfile` at appropriate locations in a script to indicate what step of the script it is at or some condition that branched. If you have multiple runs accumulating in a log file until you clear or rotate the log file, then you also want time stamps.

For your simple script it might be something like the following (which, obviously, can be extended, trimmed or modified however you like):

Code:
#!/bin/sh
cd /backups/web
mylog=/var/log/mybackup.log
timestamp=$(date +%Y%m%d%H%M%S)
echo "\nStarting regular backup at $timestamp" >> $mylog
tar -zvcf backup.web.$timestamp.tar.gz /var/www 2>&1 >> $mylog
chown digity:marines *.gz 2>>$mylog
chmod 660 *.gz 2>>$mylog
Could be mylog=/var/log/mybackup.$timestamp.log, and then remove them by age.

Last edited by choogendyk; 01-03-2010 at 02:00 PM.
 
Old 01-07-2010, 12:50 AM   #4
digity
Member
 
Registered: Apr 2005
Posts: 105

Original Poster
Rep: Reputation: 15
Wow! Thanks! Extremely helpful! I thought this was going to be complicated.

What exactly is the difference between ">> $mylog" "2>&1 >> $mylog" "2 >>$mylog"?

Also, what about adding stops or actions in addition to logging? For example, if "chown digity:marines *.gz" fails for whatever reason it'll spit the error message/code to the log or if it succeeds it'll spit "Changed owner and group successfully" to the log.
 
Old 01-14-2010, 07:33 AM   #5
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
">>" appends the output to the file (in this case $mylog). "2" refers to the error output, and "1" refers to the standard output, so "2>&1" says to send the error output to the same place the standard output is going. "2>>" indicates where to send the error output.

"$?" is the exit code for the previous command. "0" is success, anything else is an error code. So you can do an "if" statement checking the value of $?.

Code:
chown digity:marines *.gz 2>>$mylog
if [ $? ]
then
  echo "Changed owner and group successfully" >> $mylog
fi
or

Code:
if chown digity:marines *.gz 2>>$mylog
then
  echo "Changed owner and group successfully" >> $mylog
fi
Check out the guide here -- http://www.freeos.com/guides/lsst/ -- for more examples and explanation of shell scripting.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
LXer: Adding Configurable Logging to Your PHP Scripts LXer Syndicated Linux News 0 04-30-2008 10:50 AM
logging, remote ssh, scripts, and the at command gctaylor1 Programming 4 01-29-2007 08:22 AM
Logging output of /etc/rc.d init scripts damtor00 Slackware 10 04-18-2006 01:01 AM
Adding Startup Scripts kaplan71 SUSE / openSUSE 1 08-25-2005 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration