CentOS7のsystemd、systemctlについて

CentOS7のsystemd、systemctlについて

CentOS7のsystemd、systemctlについて

systemd、systemctl、ユニットについて



systemdの説明


ユニットの場所は以下です。

/etc/systemd/system
/usr/lib/systemd/system


ユニットの種類は以下などです。

.automaunt  オートマウント
.device  デバイス
.mount  マウント
.path  パス
.scope  スコープ
.service  サービス
.slice  スライス
.soket  ソケット
.swap  スワップ
.target  ターゲット
.timer  タイマー


■マニュアル・ヘルプの表示

man systemctl


systemctl -h


■grepコマンドとの併用について

systemctlの表示の中で文字列「sshd」が有る箇所を表示させるには

systemctl | grep sshd


■起動、再起動、停止
systemctl start ユニット名
systemctl restart ユニット名
systemctl stop ユニット名


※また、「service ユニット名 start」とすると、
/bin/systemctl start ユニット名.service
が実行されます。


■現在、起動しているか停止しているかの確認
systemctl status


※ユニットを指定して実行する場合は「systemctl status ユニット名」


■自動起動させる
systemctl enable ユニット名


※自動起動の停止は「systemctl disable ユニット名」


■自動起動かどうかの確認
systemctl list-unit-files


※ユニットを指定して実行する場合は「systemctl is-enabled ユニット名」


■全てのユニットの一覧を表示する

読み込まれていない(not found表示)ユニットも表示する場合

systemctl -a

または

systemctl --all


■読み込まれたユニットを一覧を表示する

読み込まれたが起動出来なかった場合はfailedと表示されます。
ユニットを停止した場合、そもそも読み込まれないため、この画面には表示されません。

systemctl
※systemctl list-units
※systemctl list-units --type=service


■読み込まれたが起動していない「failed表示」ユニットファイル一覧を表示する
systemctl --failed


■特定のターゲットにおいて有効化されているサービスを表示する

systemctl list-dependencies ターゲット名


■サービスのみを表示する
systemctl -t service

または

systemctl --type=service


■SysVサービスのみを表示する
※ネイティブなsystemdサービスは含まない。
chkconfig --list




ユニットに関するログ確認


■systemdのログ表示

journalctl


journalctl -r

※新しい順に表示


journalctl -u ユニット名

※そのユニットを指定して表示


journalctl -xn


■一般的なログの表示

tail -f /var/log/message




systemdでのファイル作成

ClamAVを設定している際、OS起動時にディレクトリを作成する必要が
あったためsystemdで作成しました。


該当ディレクトリへ移動

cd /etc/tmpfiles.d


既にサンプルとなるファイル(ここではnamed.conf)が有ったのでコピー

cp named.conf clamav.conf


vi clamav.conf

d /var/run/clamav 0755 root root -


これで起動時に正しく「/var/run/clamav」ディレクトリが作成されました。