Como posso executar um comando quando uma unidade de serviço do systemd está desabilitada?

1

Eu tenho a seguinte unidade de serviço systemd para definir os limites de bateria do meu laptop:

[Unit]
Description=Set Battery Charge Thresholds

[Service]
Type=oneshot
ExecStart=/usr/bin/tpacpi-bat -s --start 1 30
ExecStart=/usr/bin/tpacpi-bat -s --stop 1 85
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

O que eu gostaria é uma maneira de executar comandos quando eu desabilitar a unidade de serviço (para que eu possa convenientemente definir os limites de volta para seus padrões). Isso é possível?

    
por Matt 31.05.2017 / 10:54

1 resposta

0

Dê uma olhada em ExecStop ou ExecStopPost

ExecStop

Commands to execute to stop the service started via ExecStart=. This argument takes multiple command lines, following the same scheme as described for ExecStart= above. Use of this setting is optional. After the commands configured in this option are run, all processes remaining for a service are terminated according to the KillMode= setting (see systemd.kill(5)). If this option is not specified, the process is terminated by sending the signal specified in KillSignal= when service stop is requested. Specifier and environment variable substitution is supported (including $MAINPID, see above).

...

It is recommended to use this setting for commands that communicate with the service requesting clean termination. When the commands specified with this option are executed it should be assumed that the service is still fully up and is able to react correctly to all commands. For post-mortem clean-up steps use ExecStopPost= instead.

ExecStopPost

Additional commands that are executed after the service is stopped. This includes cases where the commands configured in ExecStop= were used, where the service does not have any ExecStop= defined, or where the service exited unexpectedly. This argument takes multiple command lines, following the same scheme as described for ExecStart=. Use of these settings is optional. Specifier and environment variable substitution is supported. Note that – unlike ExecStop= – commands specified with this setting are invoked when a service failed to start up correctly and is shut down again.

It is recommended to use this setting for clean-up operations that shall be executed even when the service failed to start up correctly. Commands configured with this setting need to be able to operate even if the service failed starting up half-way and left incompletely initialized data around. As the service's processes have been terminated already when the commands specified with this setting are executed they should not attempt to communicate with them.

Note that all commands that are configured with this setting are invoked with the result code of the service, as well as the main process' exit code and status, set in the $SERVICE_RESULT, $EXIT_CODE and $EXIT_STATUS environment variables, see systemd.exec(5) for details.

Ambos podem ser usados para executar comandos quando o serviço é interrompido.

As principais diferenças entre os dois:

  • ExecStop é executado enquanto o processo principal ainda está em execução e será executado somente quando o serviço for interrompido após a execução normal (ou seja, todos os serviços ExecStart e ExecStartPre devem ser bem-sucedidos e Conditions... também deve passar.

  • ExecStopPost é executado mesmo se o "serviço sair inesperadamente", o que cobre falhas. O maior benefício disso é quando você tem vários ExecStart s, se um deles for bem-sucedido e o outro falhar ExecStopPost pode ser usado para tentar limpar o sucesso parcial.

por 31.05.2017 / 12:38