Linuxなどのメモ書き

systemd & systemctl


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

概要

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

ランレベル→ターゲット

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

話はずれるが、run levelがなくなったので、/etc/inittabも使われなくなった。systemd環境でデフォルトのターゲットを指定する場合は、

ln -s /lib/systemd/system/runlevel3.target /etc/systemd/system/default.target

のように、シンボリックリンクを作成しておく。

/lib/systemd/system/にはrunlevelx.targetという形で、従来のrun levelと互換性が取れるようにファイルが用意されている(実際にはシンボリックリンクでgraphical.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を必要とするターゲットで自動起動されるようになる。

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

自動起動Off

# systemctl disable httpd.service

/sbin/checkconfig httpd off相当

 

ユニットの追加

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

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

#systemctl load foo.service

/sbin/chkconfig --add foo相当

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

enable状態のユニット一覧

/sbin/chkconfig --listのようにenable状態のユニット一覧を調べる場合は、、、

それらしき、コマンドが見当たらないので、

#ls /etc/systemd/system/multi-user.target.wants/

のようにシンボリックリンクの一覧を確認するしかないのかもしれない。


最終更新 2019/03/25 22:04:28 - kztomita
(2011/11/10 15:04:10 作成)


リンク

その他のWiki
Linuxメモ
Xnuメモ

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

よくやる仕事

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

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

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

検索

Adsense
最近のコメント