LoginSignup
1
0

More than 3 years have passed since last update.

systemdチートシート

Last updated at Posted at 2019-03-05

systemd関連のmanがいっぱいあってどれを読めばいいのかわからない

関連語 + man でgoogle検索するのもいいかもしれないが、systemdのリリース方針的に https://www.freedesktop.org/software/systemd/man/ で公開しているのは最新版のmanだし、自分のシステムにインストールされているバージョンのsystemdのmanを見たければローカルのmanを見るしかない。(他に適切な方法があれば教えてください)

ローカルでmanを探すには、

$ man systemd.index

で「systemdのmanの目次」を見ることができるので、これをとっかかりに探すのがよさそう。
(なんていう恐ろしいことになってるんだ)

systemd edit を自動化する

SYSTEMD_EDITORでエディタとして使うコマンドを指定できるので、目的の設定内容を引数で与えられたファイルに書き出すコマンドを指定すればsystemd editを自動化できる。
ここではcpコマンドであらかじめつくっておいた設定内容を、渡されたパスに書き込む。

cat > "/tmp/systemd-nspawn_override.conf" <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/systemd-nspawn --quiet --boot --link-journal=try-guest --network-veth -U --settings=override --machine=%i
EOF
SYSTEMD_EDITOR="cp ${tmp}/systemd-nspawn_override.conf" systemctl edit "systemd-nspawn@${machine}.service"

参考: https://unix.stackexchange.com/questions/459942/using-systemctl-edit-via-bash-script

serviceファイルの文法チェック

sudo systemd-analyze verify /path/to/service_file.service

ExecStartPre=はroot権限で実行してExecStart=は指定したユーザで実行する

実行パスの頭に+をつける。

systemd.service
[Service]
ExecStartPre=+/bin/mkdir -p /var/run/hoge
ExecStartPre=+/bin/chown pi /var/run/hoge
ExecStartPre=+/bin/chgrp pi /var/run/hoge
User=pi
Group=pi
ExecStart=/opt/local/hoge/main.sh

参照: systemd.serviceExecStart=の項

1
0
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
1
0