systemdのユニットファイル中のパスの前の"-"の意味

 Linux系システムのsystemdのユニットファイルを見ると、パス表現の前に"-"などの記号が付いていることがあります。
 例えばsshdの場合デフォルトでは以下のような定義になっており、[Unit]のConditionPathExistsには"!"が、[Service]のEnvironmentFileには"-"が付いています。

$ cat sshd.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
Alias=sshd.service

 個人的には"!"は反転かなと想像つくのですが、"-"は何なのか想像できませんでした。この意味を検索してみても、日本語では情報が見つけられなかったため、以下に記載します。
 

調べ方

 下手に検索せずmanコマンドでリファレンスを確認する方が手っ取り早いです。
 [Unit]及び[Install]セクションについてはsystemd.unit、[Service]セクションについてはsystemd.serviceに記載されています。

$ man systemd.unit
$ man systemd.service

 ややこしいことに、ユニットファイル中の"-"記号は常にこの意味であるという定義ではなく、ユニットファイル中の各オプションごとに各記号の意味は微妙に異なります。
 というわけで適当に抜粋して意訳します。
 

[Service]セクション

ExecStartオプション

Optionally, if this file name is prefixed with "@", the second token will be passed as "argv[0]" to the executed process, followed by the further arguments specified. If the absolute filename is prefixed with "-", an exit code of the command normally considered a failure (i.e. non-zero exit status or abnormal exit due to signal) is ignored and considered success. If both "-" and "@" are used, they can appear in either order.

  • "@"が付いていれば、2つ目のトークンを起動したプロセスの第0引数(argv[0])として引き渡す
  • "-"が付いていれば、普通は失敗と考えられる戻り値(0以外の戻り値)でも無視して成功とみなす

 となります。
 

ExecStartPre, ExecStartPostオプション

If any of those commands (not prefixed with "-") fail, the rest are not executed and the unit is considered failed.
ExecStart= commands are only run after all ExecStartPre= commands that were not prefixed with a "-" exit successfully.
Note that if any of the commands specified in ExecStartPre=, ExecStart=, or ExecStartPost= fail (and are not prefixed with "-", see above) or time out before the service is fully up, execution continues with commands specified in ExecStopPost=, the commands in ExecStop= are skipped.

  • "-"が付いていれば、コマンド実行結果が失敗でも後続コマンドを実行する

 

[Unit]セクション

ConditionNeedsUpdateオプション

takes one of /var or /etc as argument, possibly prefixed with a "!" (for inverting the condition).

  • "!"が付いていれば条件を反転する

 

ConditionPathExistsオプション

is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

  • "!"が付いていれば条件を反転し、指定したパスが存在しなければそのユニットを開始する

 

まとめ

 ざっくりと、"-"は当該コマンドの実行結果を(失敗でも)無視するような意図、"!"は条件反転といった意味になるようです。
 後者は多くのプログラミング言語でも見かける記述なので解りやすいですが、個人的には前者の意味はすぐ忘れそうな気がします。
 



以上。