Score:0

Systemd upgrade binary using reload

mx flag

I have a program that when given SIGUSR2 it would fork and create a new process gracefully (passing all existing parent socket to child, and kill parent), without downtime, so normally without systemd it would be something like this:

cp newbinary coredns
kill -s USR2 oldpid

Systemd only have ExecReload wich according to this answer

In other words, systemd wants you to only implement "reload" if the underlying service supports a true reload functionality... i.e. a reload that does not kill and restart the service, or make the service change its PID. In other words, systemd only wants to reflect what features exists.

Can it be something like this is systemd file:

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`

The question is, when doing sudo systemctl reload coredns, would systemd:

  1. know that pid changed?
  2. still handling log and process monitoring properly?
Score:0
mx flag

Ok so forking or oneoff will stuck when start, the type should be simple, it would work properly:

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 

create scripts:

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/

can be started properly:

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

remove old binary (because Text file is busy error if copied directly), then copy new binary

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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.