[HowTo] fhem als service unter systemd starten

Begonnen von betateilchen, 05 Juni 2016, 13:30:26

Vorheriges Thema - Nächstes Thema

popy

Danke für den Thread.
Hatte bei booten meines RPI2 wenn init.d verwendet wurde immer sehr viel "Network not reachable" Einträge nach einem reboot.
Zutun natürlich mit den ganzen Modulen die auf Netzwerk zugreifen (HUE, KODI usw.).
Wie oben schon kurz beschrieben sollte die Abhängigkeit vom Netzwerk in die service file geschrieben werden:

Wants=network.target
After=network.target


Meine fhem.service file schaut jetzt so aus:


[Unit]
Description=FHEM Home Automation
Wants=network.target
After=network.target

[Service]
Type=forking
Group=dialout
WorkingDirectory=/opt/fhem
ExecStart=/usr/bin/perl fhem.pl fhem.cfg
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target


Funktioniert 1a und keine Fehler mehr nach einem Reboot.

PS.: Sollte ev. angedacht werden dass bei einer fhem installation mit apt-get dies automatischa uf systemd umgestellt wird wenn vorhanden?

pOpY

volschin

In vielen Bereichen ähnlich, sieht mein derzeitiges Unit-Script etwas anders aus:
[Unit]
Description=FHEM Home Automation
After=network.target
#Wants=hmland.service

[Service]
Type=forking
User=fhem
Group=dialout
WorkingDirectory=/opt/fhem
ExecStart=/usr/bin/perl fhem.pl fhem.cfg
ExecStop=/usr/bin/perl fhem.pl 7072 "shutdown"
Restart=on-failure

[Install]
WantedBy=multi-user.target


Einfach das alte Init-Bash Script aufzurufen, mag zwar schnell gehen, verschenkt aber Potentiale von systemd.

Ich bin immer noch an dem Stop / Fehler-Teil dran und suche nach besseren Lösungen.
SIGSTOP wird ja leider in Perl nicht durchgereicht, deshalb bin ich wieder bei shutdown gelandet.
Intel NUC+Ubuntu 22.04+Docker+FHEM6
HomeMatic: HM-MOD-RPI-PCB+HM-USB-CFG2+hmland+diverse, HUE: Hue-Bridge, RaspBee+deCONZ+diverse
Amzn Dash-Buttons, Siro Rollos
4xRPi, 4xCO20, OWL+USB, HarmonyHub, FRITZ!Box 7590, Echo Dots+Show8, Logi Circle 2, HomeBridge
TIG Stack (Telegraf, InfluxDB, Grafana)

mahowi

Bei mir sieht das so aus:
[Unit]
Description=FHEM Home Automation
Documentation=http://fhem.de/commandref.html
After=network.target

[Service]
Type=forking
User=fhem
Group=dialout
WorkingDirectory=/opt/fhem
PIDFile=/var/run/fhem/fhem.pid
ExecStart=/usr/bin/perl fhem.pl configDB
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target


In global habe ich das Attribut pidfilename auf /var/run/fhem/fhem.pid gesetzt. Damit wird FHEM über die PID gestoppt.
CUBe (MAX): HT, FK | CUBe (SlowRF): ESA2000WZ
JeeLink: LaCrosse | nanoCUL433: Smartwares SHS-51001-EU, EM1000GZ
ZME_UZB1: GreenWave PowerNode, Popp Thermostat | SIGNALDuino: HE877, X10 MS14A, Revolt NC-5462,  IT Steckdosen + PIR
tado° | Milight | HUE, Lightify | SmarterCoffee

volschin

Warum arbeitest Du mit pidfile? In meinem Verständnis ist dieses Vorgehen veraltet und wird durch die Control Groups von systemd auf Kernelebene übernommen. Mir ist bisher keine pid innerhalb der cgroup abhanden gekommen.

Ich habe nochmal im Code von fhem.pl geschaut und auf den ersten Blick wird SIGTERM dort sauber mit der Shutdown-Prozedur behandelt. Damit könnte man ExecStop tatsächlich entfallen lassen.

Aus welchem Grund benutzt Du
RestartSec=5
Intel NUC+Ubuntu 22.04+Docker+FHEM6
HomeMatic: HM-MOD-RPI-PCB+HM-USB-CFG2+hmland+diverse, HUE: Hue-Bridge, RaspBee+deCONZ+diverse
Amzn Dash-Buttons, Siro Rollos
4xRPi, 4xCO20, OWL+USB, HarmonyHub, FRITZ!Box 7590, Echo Dots+Show8, Logi Circle 2, HomeBridge
TIG Stack (Telegraf, InfluxDB, Grafana)

mahowi

Das mit dem PIDfile-Eintrag habe ich der Manpage zu systemd.service entnommen:
ZitatIf set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main daemon process. This is the behavior of traditional UNIX daemons. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can identify the main process of the daemon. systemd will proceed with starting follow-up units as soon as the parent process exits.

RestartSec=5 habe ich vermutlich von irgendwoher übernommen. Das dürfte überflüssig sein.

Zumindest funktionieren start, stop und restart problemlos.
CUBe (MAX): HT, FK | CUBe (SlowRF): ESA2000WZ
JeeLink: LaCrosse | nanoCUL433: Smartwares SHS-51001-EU, EM1000GZ
ZME_UZB1: GreenWave PowerNode, Popp Thermostat | SIGNALDuino: HE877, X10 MS14A, Revolt NC-5462,  IT Steckdosen + PIR
tado° | Milight | HUE, Lightify | SmarterCoffee

volschin

#35
Zitat von: https://lists.freedesktop.org/archives/systemd-devel/2013-February/009201.htmlMAINPID is determined by systemd.

- For Type=simple/dbus/notify, it's usually the first process started.

- For Type=forking, systemd can guess based on the cgroup's contents –
each service runs in a separate cgroup (for example, run
"systemd-cgls") and systemd can use this information to find the right
process.

- But if PIDFile is specified, then systemd doesn't try to guess but
just uses the pidfile written by mysqld itself.

Der Vorteil aus meiner Sicht, alles in der cgroup wird bei Bedarf gekillt und das ist mehr als der Hauptprozess (siehe unten)
pi@ha ~ $ sudo systemctl -l status fhem
● fhem.service - FHEM Home Automation
   Loaded: loaded (/lib/systemd/system/fhem.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2017-10-30 19:23:05 CET; 56min ago
     Docs: https://fhem.de/commandref.html
  Process: 539 ExecStart=/usr/bin/perl fhem.pl fhem.cfg (code=exited, status=0/SUCCESS)
Main PID: 762 (perl)
   CGroup: /system.slice/fhem.service
           ├─  762 /usr/bin/perl fhem.pl fhem.cfg
           ├─23448 /usr/bin/perl fhem.pl fhem.cfg
           └─23461 hcitool name c0:ce:xx:xx:32:xx

Okt 30 19:23:03 ha systemd[1]: Starting FHEM Home Automation...
Okt 30 19:23:05 ha systemd[1]: Started FHEM Home Automation.
Intel NUC+Ubuntu 22.04+Docker+FHEM6
HomeMatic: HM-MOD-RPI-PCB+HM-USB-CFG2+hmland+diverse, HUE: Hue-Bridge, RaspBee+deCONZ+diverse
Amzn Dash-Buttons, Siro Rollos
4xRPi, 4xCO20, OWL+USB, HarmonyHub, FRITZ!Box 7590, Echo Dots+Show8, Logi Circle 2, HomeBridge
TIG Stack (Telegraf, InfluxDB, Grafana)

mahowi

Ich muß zugeben, daß ich mich nicht wirklich mit systemd auskenne. Ein Großteil meiner Linux-Kenntnisse stammt von vor über 15 Jahren.  8)

Zumindest ist laut Manpage die Erkennung der PID nicht immer zuverlässig bei forking:
Zitat von: https://www.freedesktop.org/software/systemd/man/systemd.service.htmlThe guessing algorithm might come to incorrect conclusions if a daemon consists of more than one process. If the main PID cannot be determined, failure detection and automatic restarting of a service will not work reliably.
[...]
In case more than one process remains, systemd will be unable to determine the main process, so it will not assume there is one. In that case, $MAINPID will not expand to anything. However, if the process decides to write a traditional PID file, systemd will be able to read the main PID from there. Please set PIDFile= accordingly.

Bringt es denn Nachteile mit sich, wenn man PIDfile verwendet?
CUBe (MAX): HT, FK | CUBe (SlowRF): ESA2000WZ
JeeLink: LaCrosse | nanoCUL433: Smartwares SHS-51001-EU, EM1000GZ
ZME_UZB1: GreenWave PowerNode, Popp Thermostat | SIGNALDuino: HE877, X10 MS14A, Revolt NC-5462,  IT Steckdosen + PIR
tado° | Milight | HUE, Lightify | SmarterCoffee

devil77

Hallo, versuche mich auch gerade an Fhem als Service und soweit läuft alles.
Wenn ich aber in fhem eine shutdown+restart möchte dann wird fhem nur gestopt.
Es erfolgt kein automatischer Start von fhem und ich muß das ganze manuell starten.
Muss man dafür noch etwas extra einstellen?
Anbei meine service Files.

fhem.service
[Unit]
Description=FHEM service
Wants=hmcfgusb.service
After=network.target

[Service]
Type=forking
User=fhem
Group=dialout
WorkingDirectory=/opt/fhem
ExecStart=/usr/bin/perl fhem.pl configDB
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target


hmcfgusb.service
Unit]
Description=HMCFGUSB

[Service]
Type=forking
ExecStart=/opt/hmcfgusb/hmland -d -p 1234 -r 0
Restart=on-failure

[Install]
WantedBy=multi-user.target

volschin

Kann ich bei mir nicht nachvollziehen.
Nov 01 13:27:00 ha systemd[1]: Starting FHEM Home Automation...
Nov 01 13:27:01 ha systemd[1]: Started FHEM Home Automation.
Nov 01 13:30:48 ha systemd[1]: fhem.service: Control process exited, code=exited status=3
Nov 01 13:30:48 ha systemd[1]: fhem.service: Unit entered failed state.
Nov 01 13:30:48 ha systemd[1]: fhem.service: Failed with result 'exit-code'.
Nov 01 13:30:53 ha systemd[1]: fhem.service: Service hold-off time over, scheduling restart.
Nov 01 13:30:53 ha systemd[1]: Stopped FHEM Home Automation.
Nov 01 13:30:53 ha systemd[1]: Starting FHEM Home Automation...
Nov 01 13:30:54 ha systemd[1]: Started FHEM Home Automation.


Um 13:30 Uhr habe ich ein "shutdown restart" ausgelöst.
Intel NUC+Ubuntu 22.04+Docker+FHEM6
HomeMatic: HM-MOD-RPI-PCB+HM-USB-CFG2+hmland+diverse, HUE: Hue-Bridge, RaspBee+deCONZ+diverse
Amzn Dash-Buttons, Siro Rollos
4xRPi, 4xCO20, OWL+USB, HarmonyHub, FRITZ!Box 7590, Echo Dots+Show8, Logi Circle 2, HomeBridge
TIG Stack (Telegraf, InfluxDB, Grafana)

betateilchen

Bevor hier ein Thread, in dem am Anfang eine funktionierende Anleitung steht, weiter durch Basteleien unbrauchbar wird, mache ich diesen Thread zu. Wer über systemd weiter diskutieren möchte, möge bitte einen eigenen Thread eröffnen.
-----------------------
Formuliere die Aufgabe möglichst einfach und
setze die Lösung richtig um - dann wird es auch funktionieren.
-----------------------
Lesen gefährdet die Unwissenheit!