Danke für das Beispiel aber das ist ja in einem Python Programm direkt implementiert.
Was ich nicht verstehe ist, wie ich das sauber in dem systemd Service mit ExecStop durchführe.
So wie ich die Erklärung von peuler in #15 verstehe, kannst Du entweder bei ExecStop ein Kommando, Script o.ä. angeben, welches Dein Programm sauber anhält.
Oder Du sorgst direkt in Deinem Programm dafür, dass es spätestens beim Empfang von SIGTERM selber sauber stoppt.
Das SIGTERM kommt ja beim Herunterfahren des OS.
Vermutlich genügt also der Einbau o.g. Funktionalität in dein autoreifeschrank.py
Ergänzung:
(aus man systemd.service)
Code
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, it is implied that the service is stopped, and any processes remaining for it 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= or RestartKillSignal= when service stop is requested. Specifier and
environment variable substitution is supported (including $MAINPID, see above).
Note that it is usually not sufficient to specify a command for this setting that only asks the service to terminate
(for example, by sending some form of termination signal to it), but does not wait for it to do so. Since the remaining
processes of the services are killed according to KillMode= and KillSignal= or RestartKillSignal= as described above
immediately after the command exited, this may not result in a clean stop. The specified command should hence be a
synchronous operation, not an asynchronous one.
Note that the commands specified in ExecStop= are only executed when the service started successfully first. They are
not invoked if the service was never started at all, or in case its start-up failed, for example because any of the
commands specified in ExecStart=, ExecStartPre= or ExecStartPost= failed (and weren't prefixed with "-", see above) or
timed out. Use ExecStopPost= to invoke commands when a service failed to start up correctly and is shut down again. Also
note that the stop operation is always performed if the service started successfully, even if the processes in the
service terminated on their own or were killed. The stop commands must be prepared to deal with that case. $MAINPID
will be unset if systemd knows that the main process exited by the time the stop commands are called.
Service restart requests are implemented as stop operations followed by start operations. This means that ExecStop= and
ExecStopPost= are executed during a service restart operation.
It is recommended to use this setting for commands that communicate with the service requesting clean termination. For
post-mortem clean-up steps use ExecStopPost= instead.
Display More
und aus man systemd.kill :
Code
KillMode=
Specifies how processes of this unit shall be killed. One of control-group, mixed, process, none.
If set to control-group, all remaining processes in the control group of this unit will be killed on unit stop (for
services: after the stop command is executed, as configured with ExecStop=). If set to mixed, the SIGTERM signal (see
below) is sent to the main process while the subsequent SIGKILL signal (see below) is sent to all remaining processes of
the unit's control group. If set to process, only the main process itself is killed (not recommended!). If set to none,
no process is killed (strongly recommended against!). In this case, only the stop command will be executed on unit stop,
but no process will be killed otherwise. Processes remaining alive after stop are left in their control group and the
control group continues to exist after stop unless empty.
Note that it is not recommended to set KillMode= to process or even none, as this allows processes to escape the service
manager's lifecycle and resource management, and to remain running even while their service is considered stopped and is
assumed to not consume any resources.
Processes will first be terminated via SIGTERM (unless the signal to send is changed via KillSignal= or
RestartKillSignal=). Optionally, this is immediately followed by a SIGHUP (if enabled with SendSIGHUP=). If processes
still remain after the main process of a unit has exited or the delay configured via the TimeoutStopSec= has passed, the
termination request is repeated with the SIGKILL signal or the signal specified via FinalKillSignal= (unless this is
disabled via the SendSIGKILL= option). See kill(2) for more information.
Defaults to control-group.
Display More