LoginSignup
6
5

More than 5 years have passed since last update.

CentOS7のサービス管理(systemd)

Posted at

はじめに

CentOS-7系を触ったら、サービス管理が"System V系のinit"から"Systemd"へと変わっていました。いろんな論争があったので知っているつもりでしたが、実際に使ってみると戸惑うことも多いので、自分が使う範囲でまとめてみました。

サービスの起動・終了

CentOS-6系で慣れている、/etc/rc.d/init.d/ 以下のスクリプトではなく、systemctl コマンドを使う。

systemctl start httpd.service   # httpd の起動
systemctl stop httpd.service     # httpd の停止
systemctl restart httpd.service  # httpd の再起動
systemctl reload httpd.service   # httpd の設定変更の反映

上の reload は、従来の /etc/init.d/httpd graceful に相当。

サービスの状態確認

$ systemctl status httpd.service
   httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2016-08-05 18:11:26 JST; 1 weeks 5 days ago

"Loaded:"行のサービスン名 /usr/lib/systemd/system/httpd.service; の後ろの enabled は自動起動の設定、"Active:" 行が現時点の実行状態をします。

自動起動設定

CeontOS6以前は chkconfig を使っていたブート時の自動起動設定も、systemctl で設定する。

systemctl enable sshd.service
systemctl disable sshd.service

自動起動設定の確認

systemctl list-unit-files 
systemctl list-unit-files -t service      # サービスリスト表示
systemctl list-unit-files --state=enabled  # 自動起動設定のリスト

STATE が enabled にならば自動起動が有効、disabledは自動起動が無効になっている。また、staticは単体で自動起動ができないサービスである。

Logの確認

systemd のログはバイナリで保存されるので、journalctl を使う。

journalctl -u httpd.service   # httpdサービスのログ表示
journalctl -b                 # ブート後のログ表示
journalctl -f                 # リアルタイム表示
journalctl --help       # ヘルプの表示

ログは、/var/run/log/journal に保存されており、保存容量などは、設定ファイル /etc/systemd/journald.conf で行う。

なお、CentOS7では、syslogd も動作しているので、/var/log/messages でもログを確認できる。

systemctl status rsyslog.service  # syslogd の動作確認

その他

困ったらまずヘルプやマニュアルを参照。それでだめなら検索。

systemctl --help
man systemctl

蛇足:以下のように、systemdのログを fluentd に流し込むことができるようです。

journalctl -f -o json | nc localhost <FLUENTD_PORT>
6
5
0

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
6
5