Skip to content

Instantly share code, notes, and snippets.

@phemmer
Created May 1, 2019 02:09
Show Gist options
  • Save phemmer/46a3d2c73f071b7216ab6adb4b833cd6 to your computer and use it in GitHub Desktop.
Save phemmer/46a3d2c73f071b7216ab6adb4b833cd6 to your computer and use it in GitHub Desktop.
Attempt to test systemd notify reload failure
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/coreos/go-systemd/daemon"
)
func main() {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan)
daemon.SdNotify(false, daemon.SdNotifyReady)
fmt.Printf("Started\n")
for {
sig := <-sigchan
switch sig {
case syscall.SIGUSR1:
fmt.Printf("Received SIGUSR1\n")
time.Sleep(time.Second)
daemon.SdNotify(false, daemon.SdNotifyReloading)
fmt.Printf("Sent reloading notification\n")
time.Sleep(time.Second)
daemon.SdNotify(false, fmt.Sprintf("ERRNO=%d", syscall.EINVAL))
fmt.Printf("Sent error\n")
case syscall.SIGTERM, syscall.SIGINT:
fmt.Printf("Received %s\n", sig)
time.Sleep(time.Second)
daemon.SdNotify(false, daemon.SdNotifyStopping)
fmt.Printf("Sent stopping notification\n")
time.Sleep(time.Second)
os.Exit(0)
default:
fmt.Printf("Unhandled signal %s\n", sig)
}
}
}
[Service]
Type=notify
ExecStart=/tmp/test
ExecReload=/bin/kill -USR1 $MAINPID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment