cron にバッチを登録する時、リダイレクトとかだらだら長く書きたくないとか、
リダイレクトの書き方をミスして、何も記録されてなかったと言う事態を回避したり、
バッチのログをローテーションさせたいとか…
まぁいろいろ有りますが、全部まとめて自分用のテンプレを晒して見ます。
テンプレmybackup.sh
#!/bin/bash
# このリダイレクトの書き方は bash 限定なので
# sh とかに変更してはならない!!
# ログファイルを年月で分ける
logfile=/tmp/mybackup.log.`date +'%Y%m'`
# 出力を全てログファイルへリダイレクト
#exec 1>> ${logfile} 2>&1
# syslog に出す時はこれ
exec 1> >(logger -p local3.info) 2>&1
# syslog に出すなら、時間の出力は不要。
printf '%s %s my backup start\n' `date +'%Y-%m-%d %H:%M:%S'`
# ダンプ開始
printf '%s %s DB dump start\n' `date +'%Y-%m-%d %H:%M:%S'`
# DB のフルダンプとか…
# mysqldump ... ...
# ファイルコピー開始
printf '%s %s file copy start\n' `date +'%Y-%m-%d %H:%M:%S'`
# ファイルのコピーとか…
printf '%s %s my backup end.\n' `date +'%Y-%m-%d %H:%M:%S'`