systemctlコマンドまとめ。サービスの起動・停止・自動起動設定とchkconfigエラーの対処
CentOSやRHELの7系以降でサービス管理に使用する systemctl コマンドの使い方をまとめます。7系より前の service コマンドとは使用方法が異なるため、利用時には注意してください。
あわせて、旧来のSysV initスクリプトが残っている環境で発生する「サービス xxx は、chkconfig をサポートしていません」エラーの対処方法も紹介します。
systemctlコマンドとは?
systemctlは systemd を操作するコマンドで、サービスの起動・停止・自動起動設定の変更・状態確認ができます。
systemdは従来のinitの代替機能で、高速なシステム起動・終了や様々なシステム管理機能を提供します。従来のinitは、シンボリックリンクとしてsystemdに置き換えられています。
$ ls -l /sbin/init
lrwxrwxrwx. 1 root root 22 Mar 30 22:53 /sbin/init -> ../lib/systemd/systemd
systemctlコマンドの使用方法
systemctl [オプション] コマンド [Unit名]
主要なコマンドは次のとおりです。
| コマンド | 意味 |
|---|---|
start | サービスを起動する |
stop | サービスを停止する |
restart | サービスを再起動する |
reload | 設定ファイルを再読み込みする |
status | サービスの状態を確認する |
enable | OS起動時の自動起動を有効化する |
disable | 自動起動を無効化する |
is-enabled | 自動起動の設定を確認する |
list-unit-files | 全Unitの一覧と起動設定を表示する |
MariaDBを例にした実行例です。
# サービスの起動
systemctl start mariadb.service
# サービスの停止
systemctl stop mariadb.service
# 自動起動の有効化
systemctl enable mariadb.service
# 自動起動の無効化
systemctl disable mariadb.service
# ステータス確認
systemctl status mariadb.service
.service は省略可能です(systemctl start mariadb でも動作します)。
「サービス xxx は、chkconfig をサポートしていません」の対処方法
旧来のSysV initスクリプトを持つサービスに systemctl enable を実行すると、次のエラーが出ることがあります。
# systemctl enable jobarg-server
Synchronizing state of jobarg-server.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable jobarg-server
サービス jobarg-server は、chkconfig をサポートしていません。
解決方法:/etc/init.d/配下の起動設定ファイルを削除
原因は、/etc/init.d/ 配下に対象サービスの旧起動スクリプトが残っていて、systemd側のUnitファイル(/usr/lib/systemd/system/xxx.service)とバッティングしていることです。
systemd側のUnitファイルが存在することを確認したうえで、/etc/init.d/ 配下の旧スクリプトを削除します。
ls /usr/lib/systemd/system/jobarg-server.service # Unitファイルがあることを確認
rm /etc/init.d/jobarg-server
削除後に systemctl enable を実行すると、正常に自動起動を設定できます。
# systemctl enable jobarg-server
Created symlink /etc/systemd/system/multi-user.target.wants/jobarg-server.service → /usr/lib/systemd/system/jobarg-server.service.
まとめ
| やること | コマンド |
|---|---|
| 起動 / 停止 / 再起動 | systemctl start / stop / restart <サービス> |
| 状態確認 | systemctl status <サービス> |
| 自動起動の有効化 / 無効化 | systemctl enable / disable <サービス> |
| chkconfig非サポートエラー | /etc/init.d/ の旧スクリプトを削除(Unitファイルの存在を確認してから) |
参考: man systemctl の訳