Systemd ExecStop 在 ExecStart 之后立即运行

Systemd ExecStop 在 ExecStart 之后立即运行

我在 /etc/systemd/system/mytest.service 中有以下文件

[Unit]
Description=My Test Service
After=network.target

[Service]
Type=forking
User=mytestuser
Group=mytestuser
WorkingDirectory=/opt/mytest

ExecStart=/bin/echo "My Test Start!"

ExecStop=/bin/echo "My Test Stop!"

ExecReload=/bin/echo "My Test Restart!"

[Install]
WantedBy=multi-user.target

运行后systemctl daemon-reload可以systemctl start mytest看到以下输出journalctl -xe

Jun 29 09:44:57 localhost.localdomain systemd[1]: Starting My Test Service...
-- Subject: Unit mytest.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mytest.service has begun starting up.
Jun 29 09:44:57 localhost.localdomain echo[4370]: My Test Start!
Jun 29 09:44:57 localhost.localdomain echo[4372]: My Test Stop!
Jun 29 09:44:57 localhost.localdomain polkitd[619]: Unregistered Authentication Agent for unix-process:4364:694445 (system bus name :1.149, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_GB.UTF-8) (disconnected from
Jun 29 09:44:57 localhost.localdomain systemd[1]: Started My Test Service.
-- Subject: Unit mytest.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mytest.service has finished starting up.
--
-- The start-up result is done.

这表明 ExecStart 命令正在运行,紧接着是 ExecStop,为什么会发生这种情况?我的 .service 文件中是否缺少某些内容?

谢谢

答案1

问题是 不是echo分叉 - 没有留下任何子进程。您需要的是一个oneshot带有 的服务RemainAfterExit=true。如果您将其更改为oneshot,您将获得完全相同的行为。该RemainAfterExit部分告诉它,即使在ExecStart退出后,该服务仍应被视为正在运行,因此它不应运行ExecStop(s)。

相关内容