AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 690203
Accepted
pkaramol
pkaramol
Asked: 2022-02-12 03:06:28 +0800 CST2022-02-12 03:06:28 +0800 CST 2022-02-12 03:06:28 +0800 CST

使 systemd 服务了解配置文件中的更改

  • 772

我的systemd服务或多或少是这样的:

[Service]
ExecStart=/usr/local/bin/binary subcommand
User=my-user
Group=my-group
EnvironmentFile=/etc/my-service/config

我正在更改内容/etc/my-service/config

我应该运行daemon-reload还是重新加载服务?

请注意,我的单元文件没有ExecReload指令;这会影响systemctl reload my-service的行为吗?

systemd
  • 1 1 个回答
  • 710 Views

1 个回答

  • Voted
  1. Best Answer
    Stewart
    2022-02-12T06:45:48+08:002022-02-12T06:45:48+08:00

    既daemon-reload不会也reload不会EnvironmentFile=被您的主进程读取。

    手册页说列出的文件EnvironmentFile=将在执行该过程之前不久被读取。

    • 这表明start或restart将导致文件被读取。那是因为start或restart将执行该过程。
    • 这也暗示daemon-reload不会因为daemon-reload没有start或restart单位而产生影响。
    • 这也表明reload不会产生任何影响,因为reload不创建主进程。它只提供了向主进程发送信号以重新加载其配置的机会。如果您没有ExecReload=定义,则尤其如此。

    辅助实验

    $ systemctl --user cat env.service
    # /home/stew/.config/systemd/user/env.service
    [Service]
    ExecStart=/bin/bash -c "while true; do sleep 1; echo $EXAMPLE_ENV; done"
    EnvironmentFile=%h/env
    
    $ cat ~/env
    EXAMPLE_ENV="Hi"
    
    $ systemctl --user start env.service
    

    然后我在操作期间监控日志:

    $ journalctl --user -u env.service -f
    ...
    Feb 11 15:35:47 stewbian systemd[1108]: Started env.service.
    Feb 11 15:35:48 stewbian bash[911848]: Hi
    Feb 11 15:35:49 stewbian bash[911848]: Hi
    

    然后我更改了环境文件,看到输出没有变化

    $ sed -i -e 's/Hi/Yo/' ~/env
    ...
    Feb 11 15:37:13 stewbian bash[911848]: Hi
    Feb 11 15:37:14 stewbian bash[911848]: Hi
    Feb 11 15:37:15 stewbian bash[911848]: Hi
    

    然后我尝试 asystemctl reload并看到输出没有变化:

    $ systemctl --user reload env.service
    Failed to reload env.service: Job type reload is not applicable for unit env.service.
    ...
    Feb 11 15:38:14 stewbian bash[911848]: Hi
    Feb 11 15:38:15 stewbian bash[911848]: Hi
    

    然后我尝试 adaemon-reload并看到输出没有变化:

    $ systemctl --user daemon-reload
    ...
    Feb 11 15:38:46 stewbian bash[911848]: Hi
    Feb 11 15:38:47 stewbian bash[911848]: Hi
    

    所以然后我尝试重新启动并查看更改。

    $ systemctl --user restart env.service
    ...
    Feb 11 15:39:29 stewbian bash[911848]: Hi
    Feb 11 15:39:30 stewbian bash[911848]: Hi
    Feb 11 15:39:30 stewbian systemd[1108]: Stopping env.service...
    Feb 11 15:39:30 stewbian systemd[1108]: Stopped env.service.
    Feb 11 15:39:30 stewbian systemd[1108]: Started env.service.
    Feb 11 15:39:31 stewbian bash[912531]: Yo
    Feb 11 15:39:32 stewbian bash[912531]: Yo
    

    一件有趣的事情是添加ExecReload=/bin/bash -c 'echo $EXAMPLE_ENV到单元中。在那种情况下,我得到这个:

    Feb 11 15:58:24 stewbian bash[914611]: Hi
    Feb 11 15:58:25 stewbian bash[914611]: Hi
    Feb 11 15:58:26 stewbian systemd[1108]: Reloading env.service...
    Feb 11 15:58:26 stewbian bash[914640]: Yo
    Feb 11 15:58:26 stewbian systemd[1108]: Reloaded env.service.
    Feb 11 15:58:26 stewbian bash[914611]: Hi
    Feb 11 15:58:27 stewbian bash[914611]: Hi
    

    所以在这里你可以看到在启动之前systemd确实读过,但只会将新环境传递给新进程。它不会改变现有流程的环境。EnvironmentFile=ExecReload=

    我通过将这些bash东西放在一个单独的脚本中来重试这个,以确保 systemd 环境变量替换在解析行期间不会影响任何东西Exec*=......结果相同。


    支持文档

    man systemd.exec:

    EnvironmentFile=
       ...         
       The files listed with this directive will be read shortly before
       the process is executed (more specifically, after all processes
       from a previous unit state terminated. This means you can
       generate these files in one unit state, and read it with this
       option in the next. The files are read from the file system of
       the service manager, before any file system changes like bind
       mounts take place).
    

    man systemctl:

       reload PATTERN...
           Asks all units listed on the command line to reload their
           configuration. Note that this will reload the service-specific
           configuration, not the unit configuration file of systemd. If you
           want systemd to reload the configuration file of a unit, use the
           daemon-reload command. In other words: for the example case of
           Apache, this will reload Apache's httpd.conf in the web server,
           not the apache.service systemd unit file.
    
           This command should not be confused with the daemon-reload
           command.
    
       daemon-reload
           Reload the systemd manager configuration. This will rerun all
           generators (see systemd.generator(7)), reload all unit files, and
           recreate the entire dependency tree. While the daemon is being
           reloaded, all sockets systemd listens on behalf of user
           configuration will stay accessible.
    
           This command should not be confused with the reload command.
    
           In other words: for the example case of Apache, this will reload Apache's httpd.conf in the
           web server, not the apache.service systemd unit file.
    
           This command should not be confused with the daemon-reload command.
    

    man systemd.service:

    ExecReload=
        Commands to execute to trigger a configuration reload in the
        service. This argument takes multiple command lines, following
        the same scheme as described for ExecStart= above. Use of this
        setting is optional. Specifier and environment variable
        substitution is supported here following the same scheme as for
        ExecStart=.
    
        One additional, special environment variable is set: if known,
        $MAINPID is set to the main process of the daemon, and may be
        used for command lines like the following:
    
            ExecReload=kill -HUP $MAINPID
    
        Note however that reloading a daemon by sending a signal (as with
        the example line above) is usually not a good choice, because
        this is an asynchronous operation and hence not suitable to order
        reloads of multiple services against each other. It is strongly
        recommended to set ExecReload= to a command that not only
        triggers a configuration reload of the daemon, but also
        synchronously waits for it to complete. For example, dbus-
        broker(1) uses the following:
    
            ExecReload=busctl call org.freedesktop.DBus \
                   /org/freedesktop/DBus org.freedesktop.DBus \
                   ReloadConfig
    
    • 4

相关问题

  • journalctl 中的区分级别

  • 将默认编辑器更改为 vim for _ sudo systemctl edit [unit-file] _

  • systemd:如何在服务启动时运行脚本,而不编辑服务定义

  • 使用 systemd 看门狗支持重新启动应用程序

  • 使用键盘快捷键启动/停止 systemd 服务 [关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve