CentOS 7のシステム管理「systemd」をイチから理解するCentOS 7で始める最新Linux管理入門(2)(3/4 ページ)

» 2015年12月24日 05時00分 公開
[大喜多利哉@IT]

initとsystemdを比較 同等のコマンドでも出力が異なるものが多くある

Unitの自動起動登録と解除

 では、initとsystemdで代表的なコマンドの違いを比較します。httpdをシステム起動時に立ち上がるサービスとして登録したい場合、それを解除したい場合の「Unitの自動起動登録」を例にします。

Unitの自動起動登録と解除
(例:httpd.serviceの場合)
init systemd
登録 chkconfig httpd on systemctl enable httpd
解除 chkconfig httpd off systemctl disable httpd

 initではchkconfig httpd onとしますが、systemdではsystemctl enable httpdとなります。

 再度systemctl list-unit-filesで調べると、先ほどのdisabledからenabledに変わっていることが分かります。

$ systemctl list-unit-files | grep httpd
httpd.service                          enabled

 登録を解除するコマンドも、initではchkconfig httpd offですが、systemdではsystemctl disable httpdになります。同じくsystemctl list-unit-filesで調べると、httpd.serviceはdisabledに表示が変わりました。

$ systemctl list-unit-files | grep httpd
httpd.service                          disabled

Unitの有効/無効を確認する

 Unitの有効/無効を確認するにはどうでしょう。「rsyslog」の有効/無効を確認する場合を例にします。

Unitの有効/無効を確認(例:rsyslog) init systemd
  chkconfig rsyslog --list systemctl is-enabled rsyslog

 それぞれ以下のように表示されます。

  • init
# chkconfig rsyslog --list
rsyslog         0:off   1:off   2:on    3:on    4:on    5:on    6:off

  • systemd
# systemctl is-enabled rsyslog
enabled

Unitを起動する

 サービスを起動をする場合の表示も以下のように違います。httpdを起動する際のコマンド例を以下に記載します。

  • init
# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using c6.ec2.tokyo for ServerName
                                                           [  OK  ]
#

  • systemd
# systemctl start httpd
#
(何も表示されない)

Unitの起動状態を確認する

 起動状態の確認は、それぞれ以下のコマンドで確認します。以下は、httpdの起動状態確認を行った場合のコマンドおよび表示の例です。

  • init
# /etc/init.d/httpd status
httpd (pid  1649) is running...

  • systemd
# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
   Active: active (running) since Sun 2015-12-13 20:06:39 JST; 1min 24s ago
 Main PID: 1483 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           |-1483 /usr/sbin/httpd -DFOREGROUND
           |-1484 /usr/sbin/httpd -DFOREGROUND
           |-1485 /usr/sbin/httpd -DFOREGROUND
           |-1486 /usr/sbin/httpd -DFOREGROUND
           |-1487 /usr/sbin/httpd -DFOREGROUND
           `-1488 /usr/sbin/httpd -DFOREGROUND
Dec 13 20:06:39 c7.ec2.tokyo systemd[1]: Starting The Apache HTTP Server...
Dec 13 20:06:39 c7.ec2.tokyo systemd[1]: Started The Apache HTTP Server.

Unitを停止する

 サービスを停止する場合は、それぞれ以下のコマンドで確認します。例は同じくhttpdの場合です。

  • init
# /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]
#

  • systemd
# systemctl stop httpd
#
(何も表示されない)

Unitを再起動する

 サービスを再起動するには、それぞれ以下のコマンドを入力します。例は同じくhttpdの場合です。

  • init
# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using c6.ec2.tokyo for ServerName
                                                           [  OK  ]
#

  • systemd
# systemctl restart httpd
#
(何も表示されない)

Unitを強制終了する

 サービスを強制終了するには、それぞれ以下のコマンドを入力します。例は同じくhttpdの場合です。

  • init
# /etc/init.d/httpd status
httpd (pid  24111) is running...
# kill 24111
# /etc/init.d/httpd status
httpd dead but subsys locked

  • systemd
# systemctl kill httpd
#
(何も表示されない)

 initでは、pidを確認して、該当するpidを指定してkillする必要があります。一方のsystemdは、Unit名を指定すればkillできます。



 このように、initとsystemdではサービスの管理単位や管理方式が異なることから、同等のコマンドでも出力が異なるものが多くあります。systemdはまだ未成熟である印象を受けますし、実際のオペレーションでは慣れが必要だと思います。

 一方でsystemdは、より高度な管理オプションが用意されている、コマンド体系が統一感のある記述となっているなど、改良されている部分もあります。

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。