LoginSignup
527

More than 5 years have passed since last update.

Linuxのパッケージをアップデートしたあとrestartが必要なプロセスを見つける方法

Last updated at Posted at 2017-06-26

はじめに

脆弱性対応のためのアップデート作業というものは頻繁に発生するものですが、本番運用しているシステムではサービスへの影響を最小限にしたいものです。
アップデートした後、出来ることなら必要なプロセスだけリスタートさせることで済ませたいのですが、どのプロセスをリスタートすれば良いのか、そもそもOS再起動しないと反映されないものなのか、判断が付かない場合が少なくありません。

そのような場合のため、再起動が必要なプロセスを見つけたり、OS再起動が必要かどうかアドバイスしてくれるコマンドがあります。

Redhat系

yum-utils」パッケージに入っている「needs-restarting」コマンドを使います。

# needs-restarting -h
Usage: 
    needs-restarting: Report a list of process ids of programs that started 
                    running before they or some component they use were updated.


Options:
  -h, --help        show this help message and exit
  -u, --useronly    show processes for my userid only
  -r, --reboothint  only report whether a full reboot is required (returns 1)
                    or not (returns 0)
  -s, --services    list the affected systemd services only

RHEL・CentOSの場合、「-r、-s」オプションが追加されたのはver7.3からです。 ⇒リリースノート

1.インストール

# yum install yum-utils

2.試しにopensslをアップデートしてみます

# yum update openssl

3.リスタートが必要なプロセスを表示

# needs-restarting | sort -g
805 : /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-enp0s3.pid -lf /var/lib/NetworkManager/dhclient-3b2440fa-27a4-4e0e-8549-888b68d3f2d4-enp0s3.lease -cf /var/lib/NetworkManager/dhclient-enp0s3.conf enp0s3 
1011 : /usr/bin/python -Es /usr/sbin/tuned -l -P 
1023 : /usr/sbin/sshd 
1129 : /usr/libexec/postfix/master -w
1147 : pickup -l -t unix -u 
1148 : qmgr -l -t unix -u 
1296 : sshd: root@pts/0  

4.「-s, --services」を付けると、systemdのサービス名で表示

# needs-restarting -s | sort
NetworkManager.service
postfix.service
sshd.service
tuned.service

5.サービスをリスタートして、再確認します

# systemctl restart NetworkManager postfix sshd tuned

# needs-restarting
#  ← ★リスタートすべきプロセスがなくなりました★

6.但し、「-r」オプション付きで確認すると、リブートすることを勧められます。

# needs-restarting -r
Core libraries or services have been updated:
  openssl-libs -> 1:1.0.1e-60.el7_3.1

Reboot is required to ensure that your system benefits from these updates.

More information:
https://access.redhat.com/solutions/27943

メッセージに書かれているURLのページを見ると、いくつかのコアなライブラリやサービスを更新した場合は再起動を促すメッセージが表示されるようです。
Redhatナレッジベース:Which packages require a system reboot after update? <要サブスクリプション>

⇒ソースコード上では、以下の8パッケージの更新時に再起動するようメッセージが出るようになっています。
『dbus、glibc、gnutls、linux-firmware、kernel、openssl-libs、systemd、udev』
ソースコード:git://yum.baseurl.org/yum-utils.git/needs-restarting.py

尚、リリースノートには、以下(一部抜粋)のように記載されてます。

needs-restarting ユーティリティーに新しい --reboothint オプションが追加されました
今回の更新により、needs-restarting ユーティリティーに新しい --reboothint オプションが追加されました。needs-restarting --reboothint を実行すると、前回のブート時からどのコアパッケージが更新されていて (該当する場合)、リブートを勧めるかどうかを示すメッセージが出力されます。これは、システム管理者が、すべての更新を有効にするためにシステムをリブートする必要があるかを判断するのに役立ちます。このアドバイスは情報提供のみで、必ずしもシステムを直ちにリブートする必要があるということではない点に注意してください (BZ#1192946)。

systemd以外から起動したプロセスの場合

自分でソースからビルドし直接起動したプロセスのチェックもできます。以下はHTTP サーバーのh2oを起動中にバイナリファイルを上書きして「needs-restarting」でチェックした場合のものです。

# needs-restarting 
17648 : /usr/local/bin/h2o -c /etc/h2o/h2o.conf 

Debian系

「debian-goodies」パッケージに入っている「checkrestart」コマンドを使います。

Debian Manpages

NAME
checkrestart - check which processes need to be restarted after an upgrade

SYNOPSIS
checkrestart [ -hvpan ] [ -b blacklist_file ] [ -i package_name ] [ -e pid ]

1.インストール

# apt-get install debian-goodies 

2.リスタートが必要なプロセスを表示

# checkrestart -a
Found 2 processes using old versions of upgraded files
(1 distinct program)
(1 distinct packages)

Of these, 1 seem to contain systemd service definitions or init scripts which can be used to restart them.
The following packages seem to have definitions that could be used
to restart their services:
openssh-server:
        1411    /usr/sbin/sshd
        1354    /usr/sbin/sshd
These are the initd scripts:
service ssh restart

「needs-restarting」コマンドの「--reboothint」のようなオプションは無いようです。

さいごに

今回のコマンドを使うことでOS再起動をしなくてもメンテナンス出来る場面が増えるのではないかと思います。
またアップデート後のOS再起動、プロセスリスタートを忘れて反映できていない状態のチェックにも活用できます。

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
527