AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / unix / Perguntas / 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

Faça o serviço systemd entender a mudança no arquivo de configuração

  • 772

Eu tenho um systemdserviço que é mais ou menos assim:

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

Estou alterando o conteúdo de/etc/my-service/config

Devo executar daemon-reloadou apenas recarregar o serviço?

Observe que não tenho uma ExecReloaddiretiva no meu arquivo de unidade; isso afeta o systemctl reload my-servicecomportamento do 's?

systemd
  • 1 1 respostas
  • 710 Views

1 respostas

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

    Nem daemon-reloadnem reloadfará com EnvironmentFile=que seja lido pelo seu processo principal.

    A página man diz que os arquivos listados com EnvironmentFile=serão lidos pouco antes de o processo ser executado.

    • Isso sugere startou restartfará com que o arquivo seja lido. Isso porque startou restartvai executar o processo.
    • Isso também sugere daemon-reloadque não terá nenhum efeito porque daemon-reloadnão startou restarta unidade.
    • Isso também sugere reloadque não terá efeito porque reloadnão cria o processo principal. Apenas dá a oportunidade de enviar um sinal ao processo principal para recarregar sua configuração. Isso é especialmente verdadeiro se você não tiver ExecReload=definido.

    Um experimento de apoio

    $ 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
    

    Então eu monitoro o diário durante as operações:

    $ 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
    

    Então eu mudo o arquivo de ambiente e não vejo nenhuma mudança na saída

    $ 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
    

    Então eu tento a systemctl reloade não vejo nenhuma mudança na saída:

    $ 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
    

    Então eu tento a daemon-reloade não vejo nenhuma mudança na saída:

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

    Então eu tento reiniciar e ver a mudança.

    $ 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
    

    Uma coisa interessante é adicionar ExecReload=/bin/bash -c 'echo $EXAMPLE_ENVà unidade. Nesse caso eu recebo isso:

    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
    

    Então aqui você pode ver systemdque lê EnvironmentFile=antes de iniciar o ExecReload=, mas só passa o novo ambiente para o novo processo. Não altera o ambiente dos processos existentes.

    Tentei novamente colocando essas bashcoisas em um script separado para garantir que a substituição da variável de ambiente do systemd não afetasse nada durante a análise das Exec*=linhas... Mesmo resultado.


    Documentação de suporte

    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

relate perguntas

  • Níveis diferenciadores no journalctl

  • Altere o editor padrão para vim para _ sudo systemctl edit [unit-file] _

  • systemd: como posso executar um script no início de um serviço, sem editar a definição do serviço

  • Use o suporte de watchdog do systemd para reiniciar o aplicativo

  • Inicie/pare o serviço systemd usando o atalho de teclado [fechado]

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    Possível firmware ausente /lib/firmware/i915/* para o módulo i915

    • 3 respostas
  • Marko Smith

    Falha ao buscar o repositório de backports jessie

    • 4 respostas
  • Marko Smith

    Como exportar uma chave privada GPG e uma chave pública para um arquivo

    • 4 respostas
  • Marko Smith

    Como podemos executar um comando armazenado em uma variável?

    • 5 respostas
  • Marko Smith

    Como configurar o systemd-resolved e o systemd-networkd para usar o servidor DNS local para resolver domínios locais e o servidor DNS remoto para domínios remotos?

    • 3 respostas
  • Marko Smith

    apt-get update error no Kali Linux após a atualização do dist [duplicado]

    • 2 respostas
  • Marko Smith

    Como ver as últimas linhas x do log de serviço systemctl

    • 5 respostas
  • Marko Smith

    Nano - pule para o final do arquivo

    • 8 respostas
  • Marko Smith

    erro grub: você precisa carregar o kernel primeiro

    • 4 respostas
  • Marko Smith

    Como baixar o pacote não instalá-lo com o comando apt-get?

    • 7 respostas
  • Martin Hope
    user12345 Falha ao buscar o repositório de backports jessie 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl Por que a maioria dos exemplos do systemd contém WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky Como exportar uma chave privada GPG e uma chave pública para um arquivo 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll status systemctl mostra: "Estado: degradado" 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim Como podemos executar um comando armazenado em uma variável? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S Por que /dev/null é um arquivo? Por que sua função não é implementada como um programa simples? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 Como ver as últimas linhas x do log de serviço systemctl 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - pule para o final do arquivo 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla Por que verdadeiro e falso são tão grandes? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis Substitua a string em um arquivo de texto enorme (70 GB), uma linha 2017-12-30 06:58:33 +0800 CST

Hot tag

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

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve