Systemd - 服务如何从重新启动运行确定第一次运行?

s.p*_*zko 3 linux embedded systemd

我想要一个在第一次运行和重新启动服务时表现不同的服务。这可以用 systemd 实现吗?(我在嵌入式操作系统中使用 systemd)。

我尝试使用 ExecReload 和 ExecStart,但仅当我使用命令“systemctl restart”时才运行 ExecReload。另一方面 ExecStart 在服务重启后运行(我有 Restart=on-failure 和 RestartSec=5)。

meu*_*euh 5

您可以使用systemctl set-environment将一些值推送到服务的未来运行中。例如,对于一个单位:

[Unit]
Description=testing
[Service]
Type=oneshot
ExecStart=/my/command myarg1 ${MYDONE}
ExecStart=/usr/bin/systemctl set-environment MYDONE=1
[Install]
Run Code Online (Sandbox Code Playgroud)

在第一个systemctl start <unit>最后ARG传递给/my/command''MYDONE不会的环境。在起步较晚,最后ARG会1MYDONE=1将在环境中。

  • 如果 `Type=oneshot`,你只能使用多个 `ExecStart`。但是对于任何类型,还有 `ExecStartPre` 和 `ExecStartPost`。 (2认同)