LoginSignup
34
30

More than 5 years have passed since last update.

systemdわからん

Posted at

かったので簡単に調べた。相変わらずのチラ裏メモなのでご容赦。

まず、従来のLinuxではカーネルが立ち上がったあとにinitプロセスというのが最初に立ち上がるりこれがすべてのプロセスの親プロセスになっていろんなデーモンやらを起動させていく、というプロセス(これは単に「過程」の意)を踏んでいたらしい。そして
Ubuntu 15.04以降ではこのinitプロセスにsystemdが採用されたということだ。

System Init Daemon
This has changed as part of the Ubuntu 15.04 devel cycle.
Ubuntu 15.04 (using Systemd by default):
Systemd runs with PID 1 as /sbin/init.
Upstart runs with PID 1 as /sbin/upstart.

Ubuntu公式wikiより。また実際、Ubuntu15.04では

Neko-no-te% ls -l /sbin/init
lrwxrwxrwx 1 root root 20  6月  3 08:29 /sbin/init -> /lib/systemd/systemd

となっていた。

次に各処理の扱いについて。systemdでは各処理を"Unit"という単位で扱っている。このUnitにはいくつか種類があって、例えばservice(とりあえず、特定の役割を果たすための一連のプロセス(または群)という理解をしています)を管理するUnitは拡張子が.serviceで終わるファイル担っていたりする。また自動生成されるユニットもあって、それらについては設定ファイルなどは存在しない模様。現在実行中のUnit、実行可能なUnitファイルはそれぞれ、

systemctl
systemctl list-unit-files

コマンドで確認することができる。

ユーザーが新たにUnitを定義する場合には、/etc/systemd/system/以下に設定ファイルを書く。例えば、apt-getで入ったnginxのserviceファイルは以下のようになっていた(これは/lib/systemd/system/以下にあった。ディレクトリ配置については多分いままでのLinux哲学に準じたものになっていると思われる)。


# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

Unitファイルの記述については適当にググること。以下参考になりそうなページ。

http://itpro.nikkeibp.co.jp/article/COLUMN/20140708/569987/?ST=oss&P=1
https://sites.google.com/site/kandamotohiro/systemd/man-systemd-unit-no-yi
https://wiki.archlinuxjp.org/index.php/Systemd

簡単な使用方法としては、

systemctl (-t TYPE) # ->現在稼働している(TYPE:たとえばserviceタイプとかの)Unitを列挙

systemctl list-unit-files (-t TYPE) # ->現在設定ファイルが用意されている(TYPE:たとえばserviceタイプとかの)Unitを列挙

systemctl enable/disable <unit name> # -> 自動起動の有効化、無効化

systemctl stop/start <unit name> # -> Unitの停止、開始

systemctl status <service name> # -> serviceの稼動状態確認

systemctl kill -s9 <service name> # -> serviceに含まれるプロセスをすべてkill

あたりを知っておけばとりあえず良いだろう。

systemdと連携したロギングシステムとしてjournaldというのが働いているらしいが、今回は詳しく取り上げない。とりあえず、-uオプションでUnit名を指定することで、特定のUnitのログにアクセスできるようだ。


Neko-no-te% journalctl -u nginx.service
-- Logs begin at 月 2015-09-28 15:26:48 JST, end at 月 2015-09-28 22:25:59 JST. --
 9月 28 15:26:50 Neko-no-te systemd[1]: Starting A high performance web server and a reverse proxy server...
 9月 28 15:26:50 Neko-no-te nginx[805]: nginx: [emerg] unknown directive "more_set_headers" in /etc/nginx/nginx.conf:27
 9月 28 15:26:50 Neko-no-te nginx[805]: nginx: configuration file /etc/nginx/nginx.conf test failed
 9月 28 15:26:50 Neko-no-te systemd[1]: nginx.service: control process exited, code=exited status=1
 9月 28 15:26:50 Neko-no-te systemd[1]: Failed to start A high performance web server and a reverse proxy server.
 9月 28 15:26:50 Neko-no-te systemd[1]: Unit nginx.service entered failed state.
 9月 28 15:26:50 Neko-no-te systemd[1]: nginx.service failed.

これから各種サービス管理のメインストリームになっていくみたいなので、それなりに触れ合っていきたい。

34
30
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
34
30