修改系统单位文件而不更改上游单位文件


24

我已经通过安装了pimd服务apt。它带有上游systemd单元文件(/lib/systemd/system/pimd.service)。

我希望该服务由于某种原因而被终止时重新启动,因此我希望Restart = always在单元文件中添加该行。

但是,我不想修改上游单元文件。

有什么解决方法吗?



你尝试了什么?
030

Answers:


35

您有两种选择:

  • 将单位文件从复制/lib/systemd/system//etc/systemd/system/
    然后进行修改,/etc/systemd/system/pimd.service以完全覆盖软件包维护者提供的单位文件。

    该命令会systemctl edit --full <service-name>自动为您执行此操作。

  • 您可以更改或添加单元的特定配置设置,而无需通过.conf在插入目录中创建文件 来修改单元文件,/etc/systemd/system/<unit-name>.<unit-type>.d/
    即创建一个/etc/systemd/system/pimd.service.d/restart.conf

    该命令systemctl edit <service-name>将为您执行这些步骤。

看到 man systemd.unit


10
第二个选项可以通过使用变得更容易systemctl edit <service-name>
穆鲁

7
和第一个使用systemctl edit --full <service-name>
grawity

22

RHEL文档建议两种方法:

  1. 通过在以下位置创建配置目录和文件来扩展默认单位文件 /etc/systemd/system/[name-goes-here].service.d/config_name.conf

在这种情况下,文件将需要包含以下内容:

[Service]
Restart=always

这是做什么的systemctl edit [name-goes-here],它将创建该目录并override.conf在其中创建目录。

  1. 创建原单位文件的副本/usr/lib/systemd/system//etc/systemd/system/,并在其中进行更改。

我会尝试选项一,但它们都是可行的选择。无论哪种方式,请记住systemctl daemon-reload在进行更改后运行。

有关创建和修改系统单位文件的RHEL文档


0

考虑使用脚本读取上游配置,对其进行修改,然后将其吐出以插入文件。

例如,我使用Chef,这是一块红宝石(库),它解析马拉松systemd单位文件以从中获取原始的ExecStart。

require 'inifile'

module Dcos
  def get_execstart_from_unit_file
    marathon_systemd_unit_file = 
IniFile.load('/etc/systemd/system/dcos-marathon.service')
    return marathon_systemd_unit_file['Service']['ExecStart']
  end
end

然后在配方中,我创建一个插入文件,将一个选项附加到ExecStart

chef_gem 'inifile'

exec_start_orig = get_execstart_from_unit_file

systemd_service_drop_in 'dcos-marathon' do
  override 'dcos-marathon.service'
  precursor 'Service' => { 'ExecStart' => nil }
  service do
    exec_start exec_start_orig + ' --env_vars_prefix "DCOS_MARATHON_"'
  end
end
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.