LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   adding logging to shell scripts (https://www.linuxquestions.org/questions/programming-9/adding-logging-to-shell-scripts-779324/)

digity 01-01-2010 08:19 PM

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

jlinkels 01-01-2010 08:52 PM

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

choogendyk 01-03-2010 09:49 AM

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.

digity 01-07-2010 12:50 AM

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.

choogendyk 01-14-2010 07:33 AM

">>" 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.


All times are GMT -5. The time now is 04:40 AM.