windows_schtasks.go 561 B

12345678910111213141516171819202122232425
  1. package win
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func SCQueryStatus(serviceName string) (state string, err error) {
  7. args := []string{"/c", "sc", "query", serviceName}
  8. ret, err := Command("cmd.exe", args)
  9. //fmt.Printf("ret : %v\n", ret)
  10. compile := regexp.MustCompile(`(\d{1,4}):`)
  11. states := compile.FindStringSubmatch(ret)
  12. if len(states) < 2 {
  13. compile = regexp.MustCompile(` r'STATE\s+:\s+(\d+)'`)
  14. states = compile.FindStringSubmatch(ret)
  15. fmt.Printf("%v\n", states)
  16. if len(states) > 1 {
  17. state = states[1]
  18. }
  19. } else {
  20. state = states[1]
  21. }
  22. return
  23. }