将具有动态实例的 Upstart 配置转换为 Systemd 服务

Convert Upstart config with dynamic instances to Systemd service

我有一个 Upstart 服务可以 运行 多个实例。可以任意组合指定两个动态参数。

有没有办法将其转换为 systemd .service 文件?

start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [016]

instance $C,$B

exec [...] $C $B

respawn

systemd 支持模板,可用于使用单个模板启动服务的多个实例。来自 man systemd.unit 中的文档:

Optionally, units may be instantiated from a template file at runtie. This allows creation of multiple units from a single configuration file. If systemd looks fora unit configuration file, it will first search for the literal unit name in the file system. If tat yields no success and the unit name contains an "@" character, systemd will look for a unittemplate that shares the same name but with the instance string (i.e. the part between the "@" character an the suffix) removed. Example: if a service getty@tty3.service is requested and no file by that names found, systemd will look for getty@.service and instantiate a service from that configuratin file if it is found.

To refer to the instance string from within the configuration file you may use the special "%i" specifier in many of the configuration options.

在您的情况下,为动态参数 "c" 和 "b" 启动服务可能如下所示:

 systemctl start myservice@b-c.service

由于 systemd 仅支持实例名称中的一个变量,您可能需要编写包装器 shell 脚本来解压缩实例名称 "b-c" 以分隔 "b" 和 "c" 传递给您的基础服务。