/Linuxコマンド

プロセスとシステム

サービスとログ(systemd)

systemctl でサービスを起動・停止・自動起動し、journalctl でログを読む。cron による定期実行も。

systemctlサービスを操作する

サービス(常に動いているプログラム)を起動・停止したり、自動起動を設定したりします。

書き方意味
systemctl status <名前>状態と最近のログを表示
sudo systemctl start <名前>起動
sudo systemctl stop <名前>停止
sudo systemctl restart <名前>再起動
sudo systemctl reload <名前>設定の読み直し(対応しているもののみ)
sudo systemctl enable --now <名前>自動起動を有効にして、今すぐ起動
sudo systemctl disable <名前>自動起動をやめる
systemctl is-active <名前>動いているか(スクリプト向け)
systemctl list-units --type=service動いているサービスの一覧
systemctl --failed失敗したものの一覧
sudo systemctl daemon-reloadユニットファイルを書き換えたあとに読み直す
systemctl cat <名前>ユニットファイルの中身を表示
bash
$ systemctl status docker
$ sudo systemctl enable --now ssh
$ systemctl list-units --type=service --state=running

自分のサービスを作る

/etc/systemd/system/myapp.service
[Unit]
Description=My App
After=network.target

[Service]
User=alice
WorkingDirectory=/home/alice/myapp
ExecStart=/usr/bin/node server.js
Restart=on-failure
Environment=PORT=3000

[Install]
WantedBy=multi-user.target
bash
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now myapp
$ journalctl -u myapp -f

journalctlログを読む

systemd が集めたログを読みます。サービスがうまく動かないときの原因調査に使います。

書き方意味
journalctl -u <名前>そのサービスのログ
journalctl -f追いかける
journalctl -e末尾から表示
journalctl -n 100最新100行
journalctl --since "1 hour ago"1時間前から
journalctl --since today今日の分
journalctl -b今回の起動以降
journalctl -p errエラー以上だけ
journalctl --disk-usageログが使っている容量
sudo journalctl --vacuum-time=7d7日より前のログを消す
bash
$ journalctl -u nginx --since "10 min ago" -f
$ journalctl -p warning -b

crontab定期的に実行する

決まった時刻や間隔で、コマンドを自動実行するように登録します。

bash
$ crontab -e          # 自分の予定表を編集
$ crontab -l          # 一覧
出力
# 分 時 日 月 曜日 コマンド
*/5 * * * *  /home/alice/bin/check.sh           # 5分ごと
0 3 * * *    /home/alice/bin/backup.sh >> /tmp/backup.log 2>&1   # 毎日3時
0 9 * * 1-5  echo "平日の9時"                   # 月〜金の9時
@reboot      /home/alice/bin/on-boot.sh          # 起動時

systemctl --userユーザーのサービス

sudo なしで、自分用のサービスを動かせます。

~/.config/systemd/user/myapp.service
[Unit]
Description=My App (user)

[Service]
ExecStart=%h/bin/myapp
Restart=on-failure

[Install]
WantedBy=default.target
bash
$ systemctl --user daemon-reload
$ systemctl --user enable --now myapp
$ journalctl --user -u myapp -f
$ loginctl enable-linger $USER      # ログアウト中も動かし続ける

systemd タイマー — cron の代わり

/etc/systemd/system/backup.timer
[Unit]
Description=毎日3時にバックアップ

[Timer]
OnCalendar=*-*-* 03:00:00
# 止まっていた間の分は起動後に実行する(systemd の設定は行末にコメントを書けない)
Persistent=true

[Install]
WantedBy=timers.target

同じ名前の backup.service を用意して sudo systemctl enable --now backup.timer で有効にします。

bash
$ systemctl list-timers
$ systemd-analyze calendar "Mon..Fri 09:00"    # 書き方が正しいか・次はいつか

systemd-runその場でサービスとして動かす

その場でコマンドを一時的なサービスとして動かします。

bash
$ sudo systemd-run --unit=longjob "$PWD/long-task.sh"   # 一時的なサービスとして実行(絶対パスで渡す)
$ systemd-run --user --on-active=30m notify-send "30分たちました"   # 30分後に実行
$ systemd-run --scope -p MemoryMax=1G ./heavy          # メモリの上限を付けて実行

systemd-analyze起動の速さを調べる

起動にかかる時間と、時間のかかっているサービスを調べます。

bash
$ systemd-analyze                  # 起動にかかった時間
$ systemd-analyze blame | head     # 時間のかかったサービス
$ systemd-analyze critical-chain

hostnamectl / localectlホスト名とキーボード・言語

ホスト名や、言語・キーボードの設定を確認・変更します。

bash
$ hostnamectl
$ sudo hostnamectl set-hostname devbox
$ localectl status
$ sudo localectl set-locale LANG=ja_JP.UTF-8