はじめに
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/ からリンクを貼る |