CentOS 7 サービス自動起動設定

CentOS 7 のサービス自動起動について

CentOS 6 以前の場合、apache(httpd)等のサービスを自動起動させる、させないといった設定は「chkconfig」コマンドを使用していたと思いますが、CentOS 7ではサービスの管理は一部のサービスを除き「systemd」で行うといった仕様に変更となっています。

従来の「chkconfig サービス名 on | off 」のコマンドも使用できますが、「systemctl」コマンドに転送されています。

chkconfig実行結果

実際に「chkconfig」コマンドで「httpd」の自動起動設定・解除を行った結果が下記の通りとなり、それぞれ「systemctl」コマンドに転送されていることがわかります。

自動起動設定

# chkconfig httpd on
情報:'systemctl enable httpd.service'へ転送しています。
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

自動起動解除

# chkconfig httpd off
情報:'systemctl disable httpd.service'へ転送しています。
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'

chkconfig --listを実行

「chkconfig --list」コマンドを実行してみると、殆どのサービスが「systemd」管理されていてるので、表示されるサービスはすごく少なくなっています。

# chkconfig --list

注記: この出力は SysV サービスのみであり、ネイティブな systemd のサービスは含まれていません。
      systemd services. SysV 設定のデータはネイティブな systemd の設定によって上書きされます。
      systemd サービスを一覧表示するには 'systemctl list-unit-files' を使用してください。
      特定のターゲットにおいて有効化されているサービスを確認するには、
      'systemctl list-dependencies [target]' 。

iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off

 

systemdでの自動起動設定方法

「chkconfig」コマンドでも設定はできるのですが、これからのためにも「systemd」で設定出来るようにするため、その手順をまとめておきます。

「systemd」での自動起動・解除設定は「systemctl」コマンドを使用し設定を行っていきます。

「chkconfig」コマンドを実行した際にメッセージが表示されていたのですが、書式は下記の通りとなります。

書式

自動起動設定 systemctl enable サービス名.service
自動起動解除 systemctl disable サービス名.service

実際に自動起動と解除の設定を行った際のログは下記の通りになります。今回は「httpd」の設定を行ってみました。

自動起動設定

# systemctl enable httpd.service
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

自動起動解除

# systemctl disable httpd.service
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'

設定状況確認

自動起動一覧表示

自動起動の一覧表示として「chkconfig --list」同様のコマンドとしては、「systemctl list-unit-files -t service」がそれにあたります。

「-t service」としているのは、serviceの種類ユニットのみを表示させるためです。そのままでは、それ以外の種類のユニットも表示されるので、表示される数が非常に多くて見づらいので、サービスだけを確認したい場合は、この「-t service」を追加しています。

実際に「systemctl list-unit-files -t service」コマンドを実行してみます。

# systemctl list-unit-files  -t service
UNIT FILE                                   STATE   
abrt-ccpp.service                           enabled 
abrt-oops.service                           enabled 
abrt-pstoreoops.service                     disabled
abrt-vmcore.service                         enabled 
abrt-xorg.service                           enabled 
abrtd.service                               enabled 
accounts-daemon.service                     enabled 
alsa-restore.service                        static  
alsa-state.service                          static  
alsa-store.service                          static  
anaconda-direct.service                     static  

#####  以下省略  #####

「STATE」部分の表示で、現在の設定状況を確認することができます。

ステータスの意味
表示 設定状況
enable 自動起動設定有効
disable 自動起動設定無効
static 単体では自動起動できないサービス

特定サービスの設定状況確認

特定サービスの設定状況を確認するには、「systemctl list-unit-files | grep httpd」とかでも良いですが、下記のような方法でも確認することが可能です。

書式
systemctl is-enabled サービス名.service

※「.service」の部分は省略もできます。

systemctl is-enabled実行結果

「httpd」の自動起動設定状態を「systemctl is-enable」コマンドで確認をした操作ログとなります。

# systemctl is-enabled httpd
enabled

「enabled」と表示され、自動起動設定が有効になっていることが確認できます。

コメント

タイトルとURLをコピーしました