diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index f4da55e60..a0fca0e79 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -78,7 +78,7 @@ The flags are: -debug print metrics as they're generated to stdout -quiet run in quiet mode -version print the version to stdout - -service Control the service, ie, 'telegraf -service install' + -service Control the service, ie, 'telegraf -service install (windows only)' In addition to the -config flag, telegraf will also load the config file from an environment variable or default location. Precedence is: @@ -110,6 +110,7 @@ var logger service.Logger var stop chan struct{} var srvc service.Service +var svcConfig service.Config type program struct{} @@ -201,6 +202,9 @@ func reloadLoop(stop chan struct{}, s service.Service) { } if *fService != "" && runtime.GOOS == "windows" { + if *fConfig != "" { + (*svcConfig).Arguments = []string{"-config", *fConfig} + } err := service.Control(s, *fService) if err != nil { log.Fatal(err) @@ -327,15 +331,14 @@ func (p *program) Stop(s service.Service) error { func main() { if runtime.GOOS == "windows" { - svcConfig := &service.Config{ + svcConfig = &service.Config{ Name: "telegraf", DisplayName: "Telegraf Data Collector Service", Description: "Collects data using a series of plugins and publishes it to" + "another series of plugins.", + Arguments: []string{"-config", "C:\\Program Files\\Telegraf\\telegraf.conf"}, } - svcConfig.Arguments = []string{"-config", "C:\\telegraf\\telegraf.conf"} - prg := &program{} s, err := service.New(prg, svcConfig) if err != nil { diff --git a/docs/WINDOWS_SERVICE.md b/docs/WINDOWS_SERVICE.md index 2ad2cd0f7..646829159 100644 --- a/docs/WINDOWS_SERVICE.md +++ b/docs/WINDOWS_SERVICE.md @@ -4,15 +4,23 @@ Telegraf natively supports running as a Windows Service. Outlined below is are the general steps to set it up. 1. Obtain the telegraf windows distribution -2. Create the directory C:\telegraf (if you install in a different location you - will need to edit `cmd/telegraf/telegraf.go` and change the config file - location and recompile to use your location) -3. Place the executable and the config file into C:\telegraf -4. Run `C:\telegraf\telegraf.exe --service install` as an administrator +2. Create the directory `C:\Program Files\Telegraf` (if you install in a different + location simply specify the `-config` parameter with the desired location) +3. Place the executable and the config file into `C:\Program Files\Telegraf` +4. To install the service into the Windows Service Manager, run (as an + administrator): + ```ps + C:\Program Files\Telegraf\telegraf.exe --service install + ``` 5. Edit the configuration file to meet your needs -6. Run `C:\telegraf\telegraf.exe --config C:\telegraf\telegraf.conf --test` to - check that it works -7. Run `net start telegraf` to start collecting data +6. To check that it works, run: + ```ps + C:\Program Files\Telegraf\telegraf.exe --config C:\Program Files\Telegraf\telegraf.conf --test + ``` +7. To start collecting data, run: + ```ps + net start telegraf + ``` ## Other supported operations