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 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
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
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.
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.
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
">>" 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.