1
0

nginx.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package nginx
  2. import (
  3. "github.com/0xJacky/Nginx-UI/settings"
  4. "os/exec"
  5. )
  6. func execShell(cmd string) (out string) {
  7. bytes, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
  8. out = string(bytes)
  9. if err != nil {
  10. out += " " + err.Error()
  11. }
  12. return
  13. }
  14. func execCommand(name string, cmd ...string) (out string) {
  15. bytes, err := exec.Command(name, cmd...).CombinedOutput()
  16. out = string(bytes)
  17. if err != nil {
  18. out += " " + err.Error()
  19. }
  20. return
  21. }
  22. func TestConf() (out string) {
  23. if settings.NginxSettings.TestConfigCmd != "" {
  24. out = execShell(settings.NginxSettings.TestConfigCmd)
  25. return
  26. }
  27. out = execCommand("nginx", "-t")
  28. return
  29. }
  30. func Reload() (out string) {
  31. if settings.NginxSettings.ReloadCmd != "" {
  32. out = execShell(settings.NginxSettings.ReloadCmd)
  33. return
  34. }
  35. out = execCommand("nginx", "-s", "reload")
  36. return
  37. }
  38. func Restart() (out string) {
  39. if settings.NginxSettings.RestartCmd != "" {
  40. out = execShell(settings.NginxSettings.RestartCmd)
  41. return
  42. }
  43. pidPath := GetPIDPath()
  44. daemon := GetSbinPath()
  45. out = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
  46. if daemon == "" {
  47. out += execCommand("nginx")
  48. return
  49. }
  50. out += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
  51. return
  52. }