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