LoginSignup
5
4

More than 5 years have passed since last update.

systemd Service Unit入門

Last updated at Posted at 2017-03-25

はじめに

CentOS7から導入されたsystemdと呼ばれる新しいシステム管理・サービスマネージャーが導入されました。今回は一番利用頻度が高いService Unitについて調べましたのでまとめます。

Unitとは

  • 今までサービス起動スクリプトとして管理してものが、Unitという形で定義され、様々なタイプのUnitがある
  • Unitは、/usr/lib/systemd/systemに配置されている

Service Unitとは

プロセスやデーモンの起動・停止を制御するUnitです。

httpd.serviceファイルをコードリーディングする

多くの人が利用しているApacheのhttpd.service Unitファイルにどんな内容が書かれているのが読んでいきます。

/usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[Unit]セクション

オプション 説明
Description Unitの説明文
After network.target remote-fs.target nss-lookup.target の後に起動する
Documentation ドキュメントのURI。manコマンドが記載されている。

man 5 systemd.unit で確認可能

[Service]セクション

オプション 説明
Type notifyは、プロセスの起動完了時にsd_notifyでメッセージを送信する
EnvironmentFile 環境変数ファイルの指定
ExecStart サービスの起動コマンド /usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecReload サービスのリロードコマンド /usr/sbin/httpd $OPTIONS -k graceful
ExecStop サービスの停止コマンド /bin/kill -WINCH ${MAINPID}
KillSignal サービスを停止する際に使用するシグナル SIGCONT
PrivateTmp /tmp/ /var/tmpを使用する

[Install]セクション

オプション 説明
WantedBy enable時に/etc/systemd/system/multi-user.target.wants/からリンクを貼る

参考

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