5
5

More than 5 years have passed since last update.

CRON等のバッチのロギング

Posted at

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'`
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5