LoginSignup
36
37

More than 3 years have passed since last update.

RaspberryPiでupdate/upgradeを自動化する。

Last updated at Posted at 2017-01-21

はじめに

以下の解答内容を見て2つ方法をピックアップしました。
How do I set my Raspberry Pi to automatically update/upgrade?

また動作環境は以下の通りです。

ハードウェア RaspberryPi3
OS Raspbian GNU/Linux 8.0 (jessie)

1 unattended-upgradesを使う。

インストール

sudo apt-get update 
sudo apt-get install unattended-upgrades //パッケージ
sudo dpkg-reconfigure -plow unattended-upgrades  //自動設更新ァイル  ※GUIがでるのでYESを選択して下さい。

unattended-upgradesを使うと次のような事が出来ます。

  • 自動アップデート
  • 自動アップデート前にメールで通知を出してくれる。
  • アップデート中にエラーが起きた場合もメール通知を出してくれる。
  • 時間指定での再起動(reboot)

設定

unattended-upgradesの設定ファイルは/etc/apt/apt.conf.d/50unattended-upgradesにあります。

cd  /etc/apt/apt.conf.d/
sudo vim 50unattended-upgrades

アップデート対象の設定

unattended-upgradesとdpkg-reconfigure -plow unattended-upgradesをインストールした時点で自動アップデートされる環境は整っていますが、アップデート対象を設定したい場合は50unattended-upgradesにかかれている以下の内容を見て、環境に合わせて記述して下さい。

Lines below have the format format is "keyword=value,...". A
package will be upgraded only if the values in its metadata match
all the supplied keywords in a line. (In other words, omitted
keywords are wild cards.) The keywords originate from the Release
file, but several aliases are accepted. The accepted keywords are:

下の行には、 "keyword = value、..."という形式の書式があります。 パッケージは、メタデータの値が、用意されていうるキーワード一の全ての行と一致する場合にのみアップグレードを行います。(言い換えれば、省略されているキーワードはワイルドカードになります。)。キーワードはリリースに由来しますが、いくつかのエイリアスが受け入れられます。 受け入れられるキーワードは次のとおりです。

    a,archive,suite (eg, "stable") 
    c,component     (eg, "main", "crontrib", "non-free")
    l,label         (eg, "Raspbian", "Raspbian-Security")
    o,origin        (eg, "Raspbian", "Unofficial Multimedia Packages")
    n,codename      (eg, "jessie", "jessie-updates")
    site          (eg, "http.Raspbian.net")`
   

インストールした時点で標準では以下の2つの設定がありますので、環境に合わせてコメントアウトして下さい。

  • // "o=Raspbian,n=jessie";
  • jessyに関連するパッケージをアップグレードする場合
  • // "o=Raspbian,a=stable";
  • セキュリティーアップデートの対象パッケージをのみアップデートグレードしたい場合

自動アップデートされたくないパッケージを指定する。

勝手にアップデートされたくないパッケージはUnattended-Upgrade::Package-Blacklistに列挙して下さい。

// List of packages to not update (regexp are supported)
Unattended-Upgrade::Package-Blacklist {
//      "vim";
//      "libc6";
//      "libc6-dev";
//      "libc6-i686";
};

メール通知を行う。

メール通知機能を実行する際は実際に、、メールサーバ(SMTPサーバ)を事前に用意してください。
アップデート時にメール通知を行いたい場合はUnattended-Upgrade::Mailに自分のメールアドレスを記入して下さい。

// Send email to this address for problems or packages upgrades
// If empty or unset then no email is sent, make sure that you
// have a working mail setup on your system. A package that provides
// 'mailx' must be installed. E.g. "user@example.com"
//Unattended-Upgrade::Mail "root";  //←コメントアウトを外す。

更にエラーの時にだけ、メール通知を送って欲しい場合はUnattended-Upgrade::MailOnlyOnErrorの項目のコメントアウトを外して下さい。

// Set this value to "true" to get emails only on errors. Default
// is to always send a mail if Unattended-Upgrade::Mail is set
//Unattended-Upgrade::MailOnlyOnError "true"; //コメントアウトを外す

再起動の時間指定

セキュリティーアップデート上、再起動が必要性ものもあります。その再起動も時間指定で自動化します。以下の様に設定ファイルを修正てし下さい。

// Automatically reboot *WITHOUT CONFIRMATION* if
//  the file /var/run/reboot-required is found after the upgrade
//Unattended-Upgrade::Automatic-Reboot "false"; 
Unattended-Upgrade::Automatic-Reboot "true";  


// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
//Unattended-Upgrade::Automatic-Reboot-Time "02:00"; //コメントアウトを外す。

Unattended-Upgrade::Automatic-Reboot-Time "02:00"; 

unattended-upgradesが行っている具体的な内容は以下の資料でご確認下さい。

2 crontab で回す。

sudo su 
crontab -e
0 7 * * 1 apt-get update && apt-get upgrade -y

左の設定項目は左から
「分」「時」「日」「月」「曜日」
となります。

0(分) 7(時) * * 1(曜日)

  • *は指定なし
  • 1曜日は月曜日を指します。

従って上記のコードは
毎週月曜日のAM7時にapt-get updateapt-get upgrade -yを実行。
と言う事になります。

時間指定の具体的な数値は以下の表を参考にして下さい。

設定可能な数値

設定項目 数値
0 -59
0-23
1-31
1-12
曜日 0-7 (0または7は日曜日)0=日、1=月、2=火、3=水、4=木、5=金、6=土、7=日

実行時間指定方法

指定方法 設定例 説明
リスト 0,10,20,30 分フィールド指定した場合は0,10,20,30分に実行します。
範囲 1-5 月フィールドで指定した場合、1,2,3,4,5月に処理を実行します
共存 1,6,9-11 時間フィールドで指定した場合、1時、6時、9時、10時、11時に処理を実行します。
間隔 */10 分フィールドで指定した場合、10分間隔で処理を実行します。「/」の後ろに指定した値の間隔で処理を実行します。

それ以外にも詳細な設定や指定方法は以下のサイトが参考になるので良かったらご覧下さい。

crontabの書き方

参考資料

36
37
6

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
36
37