LoginSignup
61

More than 5 years have passed since last update.

RaspberryPi3で初めてcrontabを使う前に

Posted at

■はじめに

Raspberryでは、とりあえずでcrontabに内容を記載しても何も起こりません(でした)。初めてcrontabを利用する前に必要な設定をまとめます。

 1. rsyslogの設定
 2. cronログレベルの設定
 3. cronログの確認
 おまけ cron内の言語設定

■1. rsyslogの設定

システムログ制御デーモンのrsyslogのcronログ出力設定を確認します。

①/etc/rsyslog.confの設定
rsyslog.conf内のcron記載がコメントアウトされているようなら、コメントアウトを外します。

設定ファイル: /etc/rsyslog.conf

/etc/rsyslog.conf(抜粋)
###############
#### RULES ####
###############

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*         /var/log/auth.log
*.*;auth,authpriv.none      -/var/log/syslog
#cron.*             /var/log/cron.log
daemon.*            -/var/log/daemon.log
kern.*              -/var/log/kern.log
lpr.*               -/var/log/lpr.log
mail.*              -/var/log/mail.log
user.*              -/var/log/user.log

↓cronのコメントアウトを外す

/etc/rsyslog.conf(抜粋)
cron.*              /var/log/cron.log

②rsyslogの再起動
設定変更後に、デーモンの再起動を行います。

sudo /etc/init.d/rsyslog restart

■2. cronログレベルの設定

①cronログレベルの設定
cronログレベルの設定です。ログレベルは設定内容の合計値で設定します。今回は「EXTRA_OPT="-L 15"」を設定します。

設定ファイル: /etc/default/cron

/etc/default/cron
# Cron configuration options

# Whether to read the system's default environment files (if present)
# If set to "yes", cron will set a proper mail charset from the
# locale information. If set to something other than 'yes', the default
# charset 'C' (canonical name: ANSI_X3.4-1968) will be used.
#
# This has no effect on tasks running under cron; their environment can
# only be changed via PAM or from within the crontab; see crontab(5).
READ_ENV="yes"

# Extra options for cron, see cron(8)
#
# For example, to enable LSB name support in /etc/cron.d/, use
# EXTRA_OPTS='-l'  
#
# Or, to log standard messages, plus jobs with exit status != 0:
# EXTRA_OPTS='-L 5'
#
# For quick reference, the currently available log levels are:
#   0   no logging (errors are logged regardless)
#   1   log start of jobs
#   2   log end of jobs
#   4   log jobs with exit status != 0
#   8   log the process identifier of child process (in all logs)
#
#EXTRA_OPTS=""

↓cronログレベルを設定

/etc/default/cron(抜粋)
EXTRA_OPTS="-L 15"

②cronの再起動
設定変更後に、デーモンの再起動を行います。

sudo /etc/init.d/cron restart

■3. cronログの確認

以下に設定ファイルが生成されたことを確認します。

ログファイル: var/log/cron.log

root@raspberrypi:~# ls var/log/cron.log

■おまけ cron内の言語設定

cron内では言語設定がされていないため、cron内に日本語を利用する場合、あらかじめ言語設定を書いておく必要があります。

言語設定: LANG=ja_JP.UTF-8
設定コマンド: crontab -u <ユーザ名> -e
参照コマンド: crontab -u <ユーザ名> -l

root@raspberrypi:~# crontab -u pi -l

LANG=ja_JP.UTF-8
30 2 1 * * echo "日本語" >> japanese.txt
~後略~

※piユーザのcrontabを設定

■出典/参考

cron の使い方
http://landisk.kororo.jp/diary/30_cron.php

cronのログを出すように設定する
http://shima-nigoro.hatenablog.jp/entry/2016/06/12/182903

cronでスクリプトを定期実行させるときに注意したい4つのこと
https://orebibou.com/2014/12/cron%E3%81%A7%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%97%E3%83%88%E3%82%92%E5%AE%9A%E6%9C%9F%E5%AE%9F%E8%A1%8C%E3%81%95%E3%81%9B%E3%82%8B%E3%81%A8%E3%81%8D%E3%81%AB%E6%B3%A8%E6%84%8F%E3%81%97%E3%81%9F/

cronで文字化けするときの対処
http://www.koikikukan.com/archives/2013/08/12-000300.php

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
61