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

How to specify different resource limits for different instances of a systemd instantiated service?

· 52CE 我爱测

For reasons above my paygrade, I need to run multiple instances of a DB server on a physical host with a bare metal OS. I also need to limit the physical memory use of each instance. I am using a template unit file to start up a …

问题原文

原帖链接 · twblamer · CC BY-SA 3.0

For reasons above my paygrade, I need to run multiple instances of a DB server on a physical host with a bare metal OS. I also need to limit the physical memory use of each instance.

I am using a template unit file to start up a few dozen instances. I am using EnvironmentFile to specify different environment variables and command line arguments for each instance.

However, I also want to be able to specify a different memory limit for each instance. AFAICT, environment variable substitution does not work for resource limit options like MemoryHigh.

I thought drop-in files would be the answer, but I couldn't a single example of using drop-in files with an instantiated service. Is there some way I can create one drop-in file for myservice@1, a different drop-in file for myservice@2, etc?

$ cat /etc/systemd/system/myservice@.service

[Unit]
Description=Something
After=syslog.target network.target

[Service]
Type=forking    
EnvironmentFile=/etc/myservice/myservice.%i.env
ExecStart=/usr/bin/myservice $OPTIONS_FROM_ENV
MemoryHigh=4G

[Install]
WantedBy=multi-user.target

已采纳解答

原帖链接 · twblamer · CC BY-SA 3.0

I think I found what I'm looking for in this answer: https://serverfault.com/a/879705/62991

Just create a drop-in directory with the instance name, and you can override settings there just like you can for a non-instantiated service.

In the below example, instances of myservice have a 4GiB memory limit by default, but myservice@1 and myservice@2 have their own settings. Works fine with systemd 231 on Fedora 25.

$ cat /etc/systemd/system/myservice@.service
[Unit]
Description=Something
After=syslog.target network.target

[Service]
Type=forking    
EnvironmentFile=/etc/myservice/myservice.%i.env
ExecStart=/usr/bin/myservice $OPTIONS_FROM_ENV
MemoryHigh=4G

[Install]
WantedBy=multi-user.target

$ cat /etc/systemd/system/[email omitted]/99-memory.conf
[Service]
MemoryHigh=10G

$ cat /etc/systemd/system/[email omitted]/99-memory.conf
[Service]
MemoryHigh=20G

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

资料来源与适用范围

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

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

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