大网数据广告活动
服务器运维问答

systemd: Start another unit when unit exits

· 52CE 我爱测

Say I have a unit foo.service that I want run after bar.service exits (with any status, whether success or failure). I do not think that After= or Before= will help me here. How can I accomplish this within systemd and not in the…

问题原文

原帖链接 · markzz · CC BY-SA 4.0

Say I have a unit foo.service that I want run after bar.service exits (with any status, whether success or failure). I do not think that After= or Before= will help me here. How can I accomplish this within systemd and not in the script launched by systemd?

For context, in my environment, say bar.service fails and has a line OnFailure=foo.service to force foo.service to fix problems and then launch bar.service again.

已采纳解答

原帖链接 · Lenniey · CC BY-SA 4.0

You can (probably) use ExecStopPost=

Additional commands that are executed after the service is stopped. This includes cases where the commands configured in ExecStop= were used, where the service does not have any ExecStop= defined, or where the service exited unexpectedly. This argument takes multiple command lines, following the same scheme as described for ExecStart=. Use of these settings is optional. Specifier and environment variable substitution is supported. Note that – unlike ExecStop= – commands specified with this setting are invoked when a service failed to start up correctly and is shut down again.

It is recommended to use this setting for clean-up operations that shall be executed even when the service failed to start up correctly. Commands configured with this setting need to be able to operate even if the service failed starting up half-way and left incompletely initialized data around. As the service's processes have been terminated already when the commands specified with this setting are executed they should not attempt to communicate with them.

Note that all commands that are configured with this setting are invoked with the result code of the service, as well as the main process' exit code and status, set in the $SERVICE_RESULT, $EXIT_CODE and $EXIT_STATUS environment variables, see systemd.exec(5) for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-05-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。