
Ich habe ein Programm, das sich bei Angabe von SIGUSR2 aufspaltet und ohne Ausfallzeiten einen neuen Prozess erstellt (alle vorhandenen übergeordneten Sockets an das untergeordnete weitergibt und das übergeordnete System beendet). Ohne systemd sähe das normalerweise etwa so aus:
cp newbinary coredns
kill -s USR2 oldpid
Systemd hat nur ExecReload
welche gemäß dieserAntwort
Mit anderen Worten: systemd möchte, dass Sie „reload“ nur implementieren, wenn der zugrunde liegende Dienst eine echte Reload-Funktionalität unterstützt, also ein Reload, das den Dienst nicht beendet und neu startet oder den Dienst dazu veranlasst, seine PID zu ändern. Mit anderen Worten: systemd möchte nur widerspiegeln, welche Funktionen vorhanden sind.
Kann es so etwas sein wie diese Systemd-Datei:
PIDFile=/tmp/coredns.pid
Type=forking
ExecStart=./coredns -pidfile /tmp/coredns.pid
ExecStop=kill `/tmp/coredns.pid`
ExecReload=/bin/kill -s USR2 `cat /tmp/coredns.pid`
Die Frage ist, ob sudo systemctl reload coredns
systemd dabei Folgendes tun würde:
- wissen Sie, dass sich die PID geändert hat?
- Gehen Sie immer noch ordnungsgemäß mit der Protokoll- und Prozessüberwachung um?
Antwort1
Ok, forking
oder oneoff
es bleibt beim Start hängen. Der Typ sollte sein simple
, damit es richtig funktioniert:
echo '[Unit]
Description=Custom CoreDNS DNS server
After=network.target
[Service]
PermissionsStartOnly=true
LimitNOFILE=1048576
LimitNPROC=512
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
WorkingDirectory=/tmp/1
Type=simple
RemainAfterExit=yes
ExecStart=/tmp/1/start.sh
ExecStop=/tmp/1/kill.sh
ExecReload=/tmp/1/reload.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
' | sudo tee /usr/lib/systemd/system/coredns.service
sudo systemctl daemon-reload
Skripte erstellen:
mkdir -p /tmp/1
echo '#!/usr/bin/bash
kill -s USR2 `cat /tmp/1/coredns.pid`
' > /tmp/1/reload.sh
echo '#!/usr/bin/bash
kill -s USR2 `cat /tmp/1/coredns.pid`
' > /tmp/1/kill.sh
echo '#!/usr/bin/bash
/tmp/1/coredns -pidfile /tmp/1/coredns.pid -conf=/tmp/1/Corefile
' > /tmp/1/start.sh
chmod a+x /tmp/1/*.sh
chmod 777 /tmp/1 # just so that no need to set user permission on systemd
cp Corefile coredns /tmp/1/
ordnungsgemäß gestartet werden kann:
sudo systemctl start coredns
# on journalctl:
Apr 20 18:50:06 pop2204 systemd[1]: Started Custom CoreDNS DNS server.
Apr 20 18:50:06 pop2204 start.sh[1553313]: whoami Name called
Apr 20 18:50:06 pop2204 start.sh[1553313]: whoami Name called
Apr 20 18:50:06 pop2204 start.sh[1553313]: whoami Name called
Apr 20 18:50:06 pop2204 start.sh[1553313]: .:1053
Apr 20 18:50:06 pop2204 start.sh[1553313]: CoreDNS-1.9.4
Apr 20 18:50:06 pop2204 start.sh[1553313]: linux/amd64, go1.20.3, 81159d2f-dirty
alte Binärdatei entfernen (da beim direkten Kopieren die Fehlermeldung „Textdatei ist belegt“ auftritt) und dann neue Binärdatei kopieren
rm /tmp/1/coredns
cp coredns /tmp/1/coredns
sudo systemctl reload coredns
# from journalctl:
Apr 20 18:52:03 pop2204 systemd[1]: Reloading Custom CoreDNS DNS server...
Apr 20 18:52:03 pop2204 start.sh[1553313]: [INFO] SIGUSR2: Upgrading
Apr 20 18:52:03 pop2204 start.sh[1553313]: [INFO] Upgrading
Apr 20 18:52:03 pop2204 systemd[1]: Reloaded Custom CoreDNS DNS server.
Apr 20 18:52:03 pop2204 start.sh[1553726]: whoami Name called 2
Apr 20 18:52:03 pop2204 start.sh[1553726]: whoami Name called 2
Apr 20 18:52:03 pop2204 start.sh[1553726]: whoami Name called 2
Apr 20 18:52:03 pop2204 start.sh[1553726]: .:1053
Apr 20 18:52:03 pop2204 start.sh[1553313]: [INFO] Upgrade finished
Apr 20 18:52:03 pop2204 start.sh[1553726]: CoreDNS-1.9.4
Apr 20 18:52:03 pop2204 start.sh[1553726]: linux/amd64, go1.20.3, 81159d2f-dirty