From 02a5d350f932d9c51ca83986cf09aca026bdff3d Mon Sep 17 00:00:00 2001 From: Naveen Honest Raj Date: Tue, 2 Mar 2021 02:09:33 -0800 Subject: [PATCH] rcd: Added systemd notification during the 'rclone rcd' command call. This also fixes #5073. Signed-off-by: Naveen Honest Raj --- cmd/rcd/rcd.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cmd/rcd/rcd.go b/cmd/rcd/rcd.go index c12efd9e9..cc4be57f9 100644 --- a/cmd/rcd/rcd.go +++ b/cmd/rcd/rcd.go @@ -3,10 +3,13 @@ package rcd import ( "context" "log" + "sync" + sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify" "github.com/rclone/rclone/cmd" "github.com/rclone/rclone/fs/rc/rcflags" "github.com/rclone/rclone/fs/rc/rcserver" + "github.com/rclone/rclone/lib/atexit" "github.com/spf13/cobra" ) @@ -48,6 +51,22 @@ See the [rc documentation](/rc/) for more info on the rc flags. log.Fatal("rc server not configured") } + // Notify stopping on exit + var finaliseOnce sync.Once + finalise := func() { + finaliseOnce.Do(func() { + _ = sysdnotify.Stopping() + }) + } + fnHandle := atexit.Register(finalise) + defer atexit.Unregister(fnHandle) + + // Notify ready to systemd + if err := sysdnotify.Ready(); err != nil { + log.Fatalf("failed to notify ready to systemd: %v", err) + } + s.Wait() + finalise() }, }