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

Make `systemd-run` fail gracefully if the unit name already exists?

· 52CE 我爱测

I have found systemd-run which allows one to run processes in the background once off ("transient services"). I always specify the service unit name with --unit $NAME . But if I have already run the systemd-run command and my pro…

问题原文

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

I have found systemd-run which allows one to run processes in the background once off ("transient services"). I always specify the service unit name with --unit $NAME. But if I have already run the systemd-run command and my process is running, then systemd-run will fail, with non-zero exit code. Is there anyway to tell systemd-run to be more idempotent and not fail in this case?

Currently I'm doing:

 systemctl is-active $NAME || systemd-run --unit $NAME $COMMAND

Is there a better way?

This is on Ubuntu 18.04, with the current systemd for that (version 237?)

已采纳解答

原帖链接 · Denis SkS · CC BY-SA 4.0

-G, --collect

Unload the transient unit after it completed, even if it failed. Normally, without this option, all units that ran and failed are kept in memory until the user explicitly resets their failure state with systemctl reset-failed or an equivalent command. On the other hand, units that ran successfully are unloaded immediately. If this option is turned on the "garbage collection" of units is more aggressive, and unloads units regardless if they exited successfully or failed. This option is a shortcut for --property=CollectMode=inactive-or-failed, see the explanation for CollectMode= in systemd.unit(5) for further information.

systemd-run --collect --unit $NAME $COMMAND

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

资料来源与适用范围

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

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

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