github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/systemd/notify.go (about) 1 package systemd 2 3 import ( 4 "fmt" 5 "log" 6 "sync" 7 8 "github.com/coreos/go-systemd/v22/daemon" 9 "github.com/rclone/rclone/lib/atexit" 10 ) 11 12 // Notify systemd that the service is starting. This returns a 13 // function which should be called to notify that the service is 14 // stopping. This function will be called on exit if the service exits 15 // on a signal. 16 func Notify() func() { 17 if _, err := daemon.SdNotify(false, daemon.SdNotifyReady); err != nil { 18 log.Printf("failed to notify ready to systemd: %v", err) 19 } 20 var finaliseOnce sync.Once 21 finalise := func() { 22 finaliseOnce.Do(func() { 23 if _, err := daemon.SdNotify(false, daemon.SdNotifyStopping); err != nil { 24 log.Printf("failed to notify stopping to systemd: %v", err) 25 } 26 }) 27 } 28 finaliseHandle := atexit.Register(finalise) 29 return func() { 30 atexit.Unregister(finaliseHandle) 31 finalise() 32 } 33 } 34 35 // UpdateStatus updates the systemd status 36 func UpdateStatus(status string) error { 37 systemdStatus := fmt.Sprintf("STATUS=%s", status) 38 _, err := daemon.SdNotify(false, systemdStatus) 39 return err 40 }