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 |
You can write to the system log:
Code:
chown digity:marines *.gz 2>&1 | logger 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 Code:
tar -zvcf backup.web.$timestamp.tar.gz /var/www > /var/log/myback.log jlinkels |
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 |
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. |
">>" 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 Code:
if chown digity:marines *.gz 2>>$mylog |
All times are GMT -5. The time now is 04:40 AM. |