Red Hat Training
A Red Hat training course is available for RHEL 8
17.18. 使用实例化单元
可以在运行时使用单一模板配置文件实例化多个单元。"@"字符用于标记模板并与其关联。实例化的单元可以从另一个单元文件(使用 Requires
或者 Wants
选项)或者 systemctl start 命令启动
。以下列方式命名实例化服务单元:
template_name@instance_name.service
其中 template_name 代表模板配置文件的名称。将 instance_name 替换为单元实例的名称。多个实例可以指向带有通用于单元所有实例的配置选项的同一个模板文件。模板单元名称具有以下格式:
unit_name@.service
例如,单位文件中的以下 Wants
设置:
Wants=getty@ttyA.service getty@ttyB.service
首先为给定服务单元进行 systemd 搜索。如果没有找到这样的单元,"@" 和类型后缀间的部分会被忽略,systemd 搜索 getty@.service
文件,从中读取配置并启动服务。
例如, getty@.service
模板包含以下指令:
[Unit] Description=Getty on %I … [Service] ExecStart=-/sbin/agetty --noclear %I $TERM …
当从上述模板中实例化 getty@ttyA.service 和 getty@ttyB.service 时, Description
= 会被解析为 Getty on ttyA 和 Getty on ttyB。