Linuxなどのメモ書き

systemd & systemctl


Rev.10を表示中。最新版はこちら

概要

Fedora15〜では、デーモンの起動管理に従来のSystemV形式のものからsystemdを使ったものに変わっている。systemd環境でのデーモンの起動管理方法のメモ。

記述がだいぶ古くなっていたので少し修正しました(2019.3)。

ランレベル→ターゲット

systemd環境では、従来のrun levelというものはなくなり、代わりにtargetというものに置き換えられている。ターゲットはmulti-user.targetのように名前がつけられており、従来のrun level 3に相当するmulti-user.target、run level 5に相当するgraphical.targetなどがある。従来のrun levelとtargetの対応は以下のようになっている(man runlevelより)。

図1 run levelとtargetの対応

Run Level Target
0 poweroff.target
1 rescue.target
2,3,4 multi-user.target
5 graphical.target
6 reboot.target

現在のtargetを変更するにはsystemctl isolateを使う。例えば、以下のようにするとRescueモード(シングルユーザモード)に遷移する。

# systemctl isolate rescue.target

現在のtargetを取得するには、systemctl list-units --type targetを使う。

# systemctl list-units --type target
<略>
graphical.target       loaded active active Graphical Interface
<略>
multi-user.target      loaded active active Multi-User System 
<略>

ただし、targetには依存関係があり、このコマンドだと読み込んでいるtargetを全て表示してしまうので、結局どのtargetで動作しているのかわかりづらい。上の例はgraphical.targetで動作している時に実行したものだが、graphical.targetはmultu-user.targetを要求しているため、これも表示されてしまっている。

このため、従来どおりrunlevelコマンドを使ってもよい。

# runlevel
5 1

右側の数字が現在のrun level(この場合、rescue.target)になる。

システム起動時のデフォルトのtargetはsystemctl get-defaultで取得できる(systemd以前では/etc/inittabに記述してあった)。

# systemctl get-default
graphical.target

デフォルトtargetを変更するには、systemctl set-defaultを使う。

# systemctl set-default multi-user.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/multi-user.target.

話はずれるが、run levelがなくなったので、/etc/inittabも使われなくなった。ファイル自体は存在するがsystemctlを使うよう記したコメントのみ記載されている。以前は以下のように、デフォルトのターゲットを変更する場合はリンクを設定しなおすように記載されていたが、

# To set a default target, run:
#
# ln -sf /lib/systemd/system/<target name>.target /etc/systemd/system/default.target
#

現在では以下のようにget-default/set-defaultを使ってデフォルトターゲットを扱うように記載されている。

# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

 

ユニットの制御

systemdではデーモンなどの制御対象のことをユニットとよぶ。ユニットの制御はsystemctlで行う。以下にコマンドの使用例を示す。

ユニットの一覧表示

単純にsystemctlだけで呼び出すと、以下のようにactive(起動状態)のユニット一覧が表示される。

# systemctl
UNIT                      LOAD   ACTIVE SUB       JOB DESCRIPTION
<略>
fedora-l...odules.service loaded active exited        Load legacy module configu
fedora-readonly.service   loaded active exited        Configure read-only root s
fedora-s...t-late.service loaded active exited        Initialize storage subsyst
fedora-s...e-init.service loaded active exited        Initialize storage subsyst
fedora-w...torage.service loaded active exited        Wait for storage scan
ip6tables.service         loaded active exited        IPv6 firewall with ip6tabl
iptables.service          loaded active exited        IPv4 firewall with iptable

起動していない(inactive状態)ユニットも表示する場合は--allオプションをつける。

# systemctl --all

ただし、--allでもdisabled状態のユニットは表示されない。全ユニットファイルを表示したい場合はlist-unit-filesを指定する。

# systemctl list-unit-files
<略>
nfs-utils.service                           static         
nfs.service                                 disabled       
nftables.service                            disabled       
nginx.service                               enabled        
nis-domainname.service                      disabled       
oddjobd.service                             disabled       
openvpn-client@.service                     disabled  

インストールされている全ユニットファイルとdisabled/enabledの状態が表示される。/sbin/chkconfig --listに近い動作となる。

ユニットの状態表示

ユニットの現在の状態を表示するにはstatusコマンドを指定する。以下はhttpdの状態を表示させた場合。

# systemctl status httpd.service
httpd.service - The Apache HTTP Server (prefork MPM)
	  Loaded: loaded (/lib/systemd/system/httpd.service; disabled)
	  Active: inactive (dead)  <-- 起動していない
	  CGroup: name=systemd:/system/httpd.service

/etc/init.d/httpd status相当

表示からもわかるように、各ユニットの設定ファイル(従来/etc/init.d/に置かれていた相当のファイル)は/lib/systemd/に置かれている。上記、httpdの場合は、/lib/systemd/system/httpd.serviceで本ファイルを修正すれば、起動対象とするターゲットなどを指定できる。

ユニットの起動・停止

ユニットの起動

# systemctl start httpd.service

/etc/init.d/httpd start相当

ユニットの停止

# systemctl stop httpd.service

/etc/init.d/httpd stop相当

自動起動の設定

上記のstart/stopでは、システム再起動時に元に戻ってしまうので、初期状態を指定する場合はenable/disableを使用する。

自動起動On

# systemctl enable httpd.service

/sbin/checkconfig httpd on相当

どのターゲットで自動起動Onにするかは設定ファイル(例えば/lib/systemd/system/httpd.service)の[Install]セクションで指定する。/lib/systemd/system/httpd.serviceでは、以下のようになっているので、

[Install]
WantedBy=multi-user.target

multi-user.target(run level3相当)および、graphical.target(run level 5相当)などのmulti-user.targetを必要とする(*1)ターゲットで自動起動されるようになる。

なお、enableにすると、/etc/systemd/system/<WantedByで指定したターゲット名>.wants/にhttpd.serviceへのシンボリックリンクが作成される。

自動起動Off

# systemctl disable httpd.service

/sbin/checkconfig httpd off相当

 

各ユニットの設定がenable/disableなのかはsystemctl list-unit-filesで確認できる。/sbin/chkconfig --listのような動作をする。

#systemctl list-unit-files

 

(*1) 「必要とする」とはターゲット間の依存関係のことで、ユニット設定ファイルのReuires=で指定する。例えば、graphical.targetの設定ファイル/usr/lib/systemd/system/graphical.targetでは、Requires=multi-user.targetと指定しているので、graphical.targetに切り替わる際はmulti-user.targetも有効になる。このため、multi-user.targetで自動起動するように設定されたユニットはgraphical.targetにおいても自動起動される。

ユニットの追加

新たに自分でユニットを追加したい場合は、loadコマンドで行う。

foo.serviceを/lib/systemd/system/に作成しておき、、、

# systemctl load foo.service

/sbin/chkconfig --add foo相当

削除したい場合は、それらしきコマンドがないのだが、デーモンを止めて、設定ファイル自体を削除してしまえばいいみたい。

 


最終更新 2019/03/26 00:37:45 - kztomita
(2011/11/10 15:04:10 作成)


リンク

その他のWiki
Linuxメモ
Xnuメモ

会社
(有)ビットハイブ
受託開発やってます。

よくやる仕事

・Webシステム開発(LAMP環境)
・Linuxサーバー設定関連
サーバー移転作業代行

開発事例にデジタルカタログ/マンガビューワーを追加しました。

draggable.jsのスマホ対応版デモページを追加しました。説明はこちら

検索

Adsense
最近のコメント