No systemd, qual é a diferença entre After = e Requires =?

39

Estou criando um arquivo systemd .service e preciso de ajuda para entender a diferença entre Requires= e After= . A página man diz que Requires= "Configura dependências de requisitos em outras unidades." e After= "Configura as dependências de pedidos entre as unidades." Qual a diferença?

    
por TomOnTime 01.11.2016 / 20:04

2 respostas

27

After= configura a ordem de serviço (faça X somente após Y), enquanto Requires= dependências de estado. Se você não especificar um pedido, um serviço, dependendo do outro, será iniciado ao mesmo tempo que o que estiver dependendo. Além disso, do jeito que eu entendi (embora eu não possa testar isso agora e não encontrar uma referência), After= é um "acoplamento frouxo", e um serviço com tal instrução ainda seria executado se o chamado em a linha After= não foi iniciada, enquanto Require= impediria que ela fosse iniciada se o requisito não fosse atendido.

Citando o link :

Requires=

Configures requirement dependencies on other units. If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or its activation fails, this unit will be deactivated. This option may be specified more than once or multiple space-separated units may be specified in one option in which case requirement dependencies for all listed names will be created. Note that requirement dependencies do not influence the order in which services are started or stopped. This has to be configured independently with the After= or Before= options. If a unit foo.service requires a unit bar.service as configured with Requires= and no ordering is configured with After= or Before=, then both units will be started simultaneously and without any delay between them if foo.service is activated. Often, it is a better choice to use Wants= instead of Requires= in order to achieve a system that is more robust when dealing with failing services.

e

Before=, After=

A space-separated list of unit names. Configures ordering dependencies between units. If a unit foo.service contains a setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service is started up. Note that this setting is independent of and orthogonal to the requirement dependencies as configured by Requires=. It is a common pattern to include a unit name in both the After= and Requires= option, in which case the unit listed will be started before the unit that is configured with these options. This option may be specified more than once, in which case ordering dependencies for all listed names are created. After= is the inverse of Before=, i.e. while After= ensures that the configured unit is started after the listed unit finished starting up, Before= ensures the opposite, i.e. that the configured unit is fully started up before the listed unit is started. Note that when two units with an ordering dependency between them are shut down, the inverse of the start-up order is applied. i.e. if a unit is configured with After= on another unit, the former is stopped before the latter if both are shut down. Given two units with any ordering dependency between them, if one unit is shut down and the other is started up, the shutdown is ordered before the start-up. It doesn't matter if the ordering dependency is After= or Before=. It also doesn't matter which of the two is shut down, as long as one is shut down and the other is started up. The shutdown is ordered before the start-up in all cases. If two units have no ordering dependencies between them, they are shut down or started up simultaneously, and no ordering takes place.

    
por 01.11.2016 / 20:25
1

Uma das principais diferenças é

  • After apenas verifica se a unidade já está ativada e não ativa explicitamente as unidades especificadas.
  • As unidades listadas em Requires são ativadas junto com a unidade. Se alguma das unidades necessárias falhar ao iniciar, a unidade não será ativada.

Considere que eu tenho um arquivo de unidade test-app.service ,

[Unit]
Description=test app
After=network-online.target

Aqui está o que acontecerá quando esta declaração for executada,

  • After verifica se network-online.target .
  • se network-online.target não for iniciado, ele aguardará.
  • test-app começa somente depois que network-online.target está ativo

Se eu tivesse Requires ,

[Unit]
Description=test app
Requires=network-online.target

Aqui está o que acontecerá quando esta declaração for executada,

  • network-online.target e test-app são ativados juntos
  • se network-online.target falhar ao iniciar test-app não será ativado.
por 18.09.2018 / 00:43