LoginSignup
11
10

More than 5 years have passed since last update.

【Amazon Linux 2 でも使える!】systemctl 等コマンドチートシート

Last updated at Posted at 2019-02-06

コマンド systemctl のオプションってなんであんな長いんだろう…
ということでチートシートを作りました。

オプション

よく使うオプション。

--no-pager   # リスト表示するときなどにページング処理をしない
--all        # inactive なものもリストする

ランレベル

デフォルトのランレベルの設定は、従来のように /etc/inittab は編集しない。(そして以下のコマンドは、/etc/inittab でも説明されている。)

systemctl get-default                         # 確認
sudo systemctl set-default multi-user.target  # runlevel 3
sudo systemctl set-default graphical.target   # runlebel 5

一覧リスト

Unitをリストする。

systemctl list-unit --type=service
systemctl --type=service                  # そして list-unit は省略してよい
systemctl --type=target                   # 他に、 mount,swap,device,socket の type 指定もできる
systemctl --type=service --state=running  # Daemon型で running なもの

定義一覧。

systemctl list-unit-files --type=service

Unitの依存関係を確認する。

systemctl list-dependencies 
systemctl list-dependencies Unit名
systemctl list-dependencies Unit名 --after  # Unit名より先に起動するもの
systemctl list-dependencies Unit名 --before # Unit名より後に起動するもの

Unit操作

自動起動の有効化と無効化。
(以下、例:httpd.serviceについて。ユニット名はhttpdなので.serviceは省略可能)

systemctl enable httpd
systemctl disable httpd
systemctl is-enabled httpd    # 有効化状態確認

状態表示。

systemctl status httpd
systemctl status httpd -l            # 省略せずに表示
systemctl status httpd -H ホスト名    # 別ホストのを見る(ssh経由)

停止、起動、再起動。

sudo systemctl stop httpd
sudo systemctl start httpd
sudo systemctl restart httpd
sudo systemctl reload httpd

systemdの全体の設定ファイルの再読み込み。

sudo systemctl daemon-reload

Cgroups

Cgroupsごとの確認。コマンドが違うことに注意。

systemd-cgls     # Cgroupsごとにツリー形式で分類表示
systemd-cgtop    # Cgroups単位でのtopコマンドライクなリソース使用状況表示

kill

killする。

sudo systemctl kill -s9 httpd

ログ

systemd で管理されたユニットは、journalctl でログが確認できる。

ログを見る。

journalctl -u httpd
journalctl -u httpd --all               # -a と同じ。長い出力を省略しない。
journalctl -u httpd --no-pager
journalctl -u httpd -f                  # tail -f のように振る舞う
journalctl -u httpd -o json-pretty      # json形式で
journalctl -u httpd --since=yesterday   # -S でも同じ。"today""2012-10-30 18:17:16" も可
journalctl -u httpd --until=yesterday   # -U でも同じ。"today""2012-10-30 18:17:16" も可
11
10
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
11
10