diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 4c9ee08ab..de8ef6b20 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -96,6 +96,14 @@ var fServiceName = flag.String("service-name", "telegraf", var fServiceDisplayName = flag.String("service-display-name", "Telegraf Data Collector Service", "service display name (windows only)") +//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows +var fServiceAutoRestart = flag.Bool("service-auto-restart", false, + "auto restart service on failure (windows only)") + +//nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows +var fServiceRestartDelay = flag.String("service-restart-delay", "5m", + "delay before service auto restart, default is 5m (windows only)") + //nolint:varcheck,unused // False positive - this var is used for non-default build tag: windows var fRunAsConsole = flag.Bool("console", false, "run as console application (windows only)") diff --git a/cmd/telegraf/telegraf_windows.go b/cmd/telegraf/telegraf_windows.go index 13ce661b6..e857aa7e1 100644 --- a/cmd/telegraf/telegraf_windows.go +++ b/cmd/telegraf/telegraf_windows.go @@ -94,6 +94,10 @@ func runAsWindowsService(inputFilters, outputFilters []string) { //set servicename to service cmd line, to have a custom name after relaunch as a service svcConfig.Arguments = append(svcConfig.Arguments, "--service-name", *fServiceName) + if *fServiceAutoRestart { + svcConfig.Option = service.KeyValue{"OnFailure": "restart", "OnFailureDelayDuration": *fServiceRestartDelay} + } + err := service.Control(s, *fService) if err != nil { log.Fatal("E! " + err.Error()) diff --git a/docs/WINDOWS_SERVICE.md b/docs/WINDOWS_SERVICE.md index fe77a16bf..39a672c63 100644 --- a/docs/WINDOWS_SERVICE.md +++ b/docs/WINDOWS_SERVICE.md @@ -61,6 +61,10 @@ on a single system, you can install the service with the `--service-name` and > C:\"Program Files"\Telegraf\telegraf.exe --service install --service-name telegraf-2 --service-display-name "Telegraf 2" ``` +## Auto restart and restart delay + +By default the service will not automatically restart on failure. Providing the `--service-auto-restart` flag during installation will always restart the service with a default delay of 5 minutes. To modify this to for example 3 minutes, provide the additional flag `--service-restart-delay 3m`. The delay can be any valid `time.Duration` string. + ## Troubleshooting When Telegraf runs as a Windows service, Telegraf logs messages to Windows events log before configuration file with logging settings is loaded. diff --git a/internal/usage_windows.go b/internal/usage_windows.go index 9a1169851..c85f944d2 100644 --- a/internal/usage_windows.go +++ b/internal/usage_windows.go @@ -44,6 +44,8 @@ The commands & flags are: --service operate on the service (windows only) --service-name service name (windows only) --service-display-name service display name (windows only) + --service-auto-restart auto restart service on failure (windows only) + --service-restart-delay delay before service auto restart, default is 5m (windows only) Examples: @@ -73,4 +75,7 @@ Examples: # install telegraf service with custom name telegraf --service install --service-name=my-telegraf --service-display-name="My Telegraf" -` + + # install telegraf service with auto restart and restart delay of 3 minutes + telegraf --service install --service-auto-restart --service-restart-delay 3m + `