Systemd 服务和插入单元中 %j 的值不一致

Systemd 服务和插入单元中 %j 的值不一致

在 Debian 系统上,假设我有一个实例 systemd 单元:/lib/systemd/system/[email protected]

[Unit]
Description=good old foo

[Service]
Type=oneshot
ExecStart=/bin/env echo "%%i=[%i]   %%j=[%j] (from %n ([email protected]))"

我想[email protected]对我的系统做一些小的补充。因为单元文件位于/lib/systemd/system不建议编辑的地方,所以我创建了一个目录(按部就班地做事),在其中创建了一个文件,其中包含:/etc/systemd/system/[email protected]additional.conf

[Service]
ExecStart=/bin/env echo "%%i=[%i]   %%j=[%j] (from %n (additional.conf))"

重新加载 systemd conf 并运行:sudo systemctl cat [email protected]

# /lib/systemd/system/[email protected]
[Unit]
Description=good old foo

[Service]
Type=oneshot
ExecStart=/bin/env echo "%%i=[%i]   %%j=[%j] (from %n ([email protected]))"

# /etc/systemd/system/[email protected]/additional.conf
[Service]
ExecStart=/bin/env echo "%%i=[%i]   %%j=[%j] (from %n (additional.conf))"

这场演出[email protected]吸引了我的新additional.conf眼球。

正如您在单元文件中看到的,我正在使用%j说明符(这是最后一个“-”和前缀名称末尾之间的字符串)。为了具体使用它,我创建了一个符号链接:

sudo ln -sv /lib/systemd/system/[email protected] /etc/systemd/system/[email protected]
'/etc/systemd/system/[email protected]' -> '/lib/systemd/system/[email protected]'

重新加载 systemd conf 并运行:sudo systemctl cat [email protected]

# /lib/systemd/system/[email protected]
[Unit]
Description=good old foo

[Service]
Type=oneshot
ExecStart=/bin/env echo "%%i=[%i]   %%j=[%j] (from %n ([email protected]))"

# /etc/systemd/system/[email protected]/additional.conf
[Service]
ExecStart=/bin/env echo "%%i=[%i]   %%j=[%j] (from %n (additional.conf))"

这表明[email protected]我已添加内容。现在运行,然后:systemctl start [email protected]systemctl status [email protected]

[email protected] - good old foo
     Loaded: loaded (/lib/systemd/system/[email protected]; static)
    Drop-In: /etc/systemd/system/[email protected]
             └─additional.conf
     Active: inactive (dead)

Nov 01 12:10:31 Intel-NUC systemd[1]: Starting good old foo...
Nov 01 12:10:31 Intel-NUC env[56108]: %i=[baz]   %j=[bar] (from [email protected] ([email protected]))
Nov 01 12:10:31 Intel-NUC env[56109]: %i=[baz]   %j=[foo] (from [email protected] (additional.conf))
Nov 01 12:10:31 Intel-NUC systemd[1]: [email protected]: Succeeded.
Nov 01 12:10:31 Intel-NUC systemd[1]: Finished good old foo.

您可以看到,的值%i是恒定的,但%j根据单元文件的不同,在一次调用中会发生变化。对我来说,这是不直观的,而且我无法找到任何 systemd 文档来说明这种行为是预期的。

我知道我可以复制并编辑它 - 我只是想在这里使用推荐的做法。/lib/systemd/system/[email protected]/etc/systemd/system/[email protected]

是否有人知道改变值是否%j是一种可接受的行为,或者我只是在滥用/混淆 systemd 和我的符号链接?

相关内容