web-technical-blog

web開発に関する技術メモ

CentOS7 サービス自動起動の設定方法について

CentOS6以前では、「chkconfig」コマンドを使用していたが、
CentOS7ではサービスの管理は一部のサービスを除き「sysytemd」で
行う仕様に変更になったみたいです。

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

■systemdでの自動起動設定方法

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

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

自動起動解除 systemctl disable サービス名.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
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」の部分は省略可能

▼参考URL
www.server-memo.net