@@ -541,19 +541,30 @@ func RestartZimbraService(service string) bool {
541541
542542 log .Warn ().Str ("service" , service ).Msg ("Attempting to restart Zimbra services" )
543543
544- // If the affected service is 'stats', stop zmstat first
545- if strings .EqualFold (strings .TrimSpace (service ), "stats" ) {
546- log .Warn ().Msg ("Detected 'stats' service; running 'zmstatctl stop' before restart" )
547- if out , err := ExecZimbraCommand ("zmstatctl stop" , false , false ); err != nil {
548- log .Warn ().Err (err ).Str ("output" , out ).Msg ("zmstatctl stop failed; continuing with restart" )
549- }
550- }
544+ var output string
545+ var err error
551546
552- output , err := ExecZimbraCommand ("zmcontrol start" , false , false )
553- log .Debug ().Str ("output" , output ).Msg ("zmcontrol start output" )
547+ switch service {
548+ case "zmswatchctl" , "zmswatch" :
549+ ExecZimbraCommand ("zmswatchctl stop" , false , false )
550+ time .Sleep (3 * time .Second )
551+ output , err = ExecZimbraCommand ("zmswatchctl start" , false , false )
552+ case "zmlogswatchctl" , "zmlogswatch" :
553+ ExecZimbraCommand ("zmlogswatchctl stop" , false , false )
554+ time .Sleep (3 * time .Second )
555+ output , err = ExecZimbraCommand ("zmlogswatchctl start" , false , false )
556+ case "zmstatctl" , "zmstat" , "stats" :
557+ ExecZimbraCommand ("zmstatctl stop" , false , false )
558+ time .Sleep (3 * time .Second )
559+ output , err = ExecZimbraCommand ("zmstatctl start" , false , false )
560+ default :
561+ output , err = ExecZimbraCommand ("zmcontrol start" , false , false )
562+ }
563+
564+ log .Debug ().Str ("output" , output ).Msg ("restart output" )
554565 if err != nil {
555566 log .Error ().Err (err ).Str ("service" , service ).Msg ("Failed to start Zimbra services" )
556- common .AlarmCheckDown ("service_restart_failed_" + service , "Failed to execute zmcontrol start for " + service + ": " + err .Error (), false , "" , "" )
567+ common .AlarmCheckDown ("service_restart_failed_" + service , "Failed to execute restart for " + service + ": " + err .Error (), false , "" , "" )
557568 return false
558569 }
559570
@@ -678,41 +689,14 @@ func CheckZimbraServices() []ServiceInfo {
678689 currentServices , currentStatus := parseStatus (initialOutput )
679690 restartAttempted := false
680691
681- // Attempt restarts for services that are down
682692 for _ , svc := range currentServices {
683693 service := strings .TrimSpace (svc .Name )
684- switch service {
685- case "zmswatchctl" , "zmswatch" :
686- output , err := ExecZimbraCommand ("zmzmswatchctl stop" , false , false )
687- log .Debug ().Str ("output" , output ).Msg ("zmzmswatchctl stop output" )
688- if err == nil {
689- output , err = ExecZimbraCommand ("zmzmswatchctl start" , false , false )
690- log .Debug ().Str ("output" , output ).Msg ("zmzmswatchctl start output" )
691- }
692- continue
693- case "zmlogswatchctl" , "zmlogswatch" :
694- output , err := ExecZimbraCommand ("zmlogswatchctl stop" , false , false )
695- log .Debug ().Str ("output" , output ).Msg ("zmlogswatchctl stop output" )
696- if err == nil {
697- output , err = ExecZimbraCommand ("zmlogswatchctl start" , false , false )
698- log .Debug ().Str ("output" , output ).Msg ("zmlogswatchctl start output" )
699- }
700- continue
701- case "zmstatctl" , "zmstat" , "stats" :
702- output , err := ExecZimbraCommand ("zmstatctl stop" , false , false )
703- log .Debug ().Str ("output" , output ).Msg ("zmstatctl stop output" )
704- if err == nil {
705- output , err = ExecZimbraCommand ("zmstatctl start" , false , false )
706- log .Debug ().Str ("output" , output ).Msg ("zmstatctl start output" )
707- }
708- continue
709- }
710694 if ! svc .Running {
711695 log .Warn ().Str ("service" , svc .Name ).Msg ("Service is not running" )
712696 common .WriteToFile (common .TmpDir + "/" + "zmcontrol_status_" + time .Now ().Format ("2006-01-02_15.04.05" )+ ".log" , initialOutput )
713697 common .AlarmCheckDown ("service_" + svc .Name , svc .Name + " is not running ````spoiler zmcontrol status\n " + initialOutput + "\n ```" , false , "" , "" )
714698 if MailHealthConfig .Zimbra .Restart {
715- if RestartZimbraService (svc . Name ) {
699+ if RestartZimbraService (service ) {
716700 restartAttempted = true
717701 }
718702 }
@@ -975,20 +959,21 @@ func ExecZimbraCommand(command string, fullPath bool, runAsRoot bool) (string, e
975959 // Execute command
976960 if fullPath {
977961 log .Debug ().Str ("command" , command ).Msg ("Executing Zimbra command with full path" )
978- cmd = exec .Command ("/bin/su" , zimbraUser , "-c" , zimbraPath + "/" + command )
962+ cmd = exec .Command ("/bin/su" , "-" , zimbraUser , "-c" , zimbraPath + "/" + command )
979963 } else {
980964 log .Debug ().Str ("command" , command ).Msg ("Executing Zimbra command without full path" )
981- cmd = exec .Command ("/bin/su" , zimbraUser , "-c" , zimbraPath + "/bin/" + command )
965+ cmd = exec .Command ("/bin/su" , "-" , zimbraUser , "-c" , zimbraPath + "/bin/" + command )
982966 }
983967
984968 var out bytes.Buffer
969+ var stderr bytes.Buffer
985970 cmd .Stdout = & out
986- cmd .Stderr = os . Stderr
971+ cmd .Stderr = & stderr
987972 cmd .Run ()
988973
989- log .Debug ().Str ("command" , command ).Str ("output" , out .String ()).Msg ("Command executed" )
974+ log .Debug ().Str ("command" , command ).Str ("output" , out .String ()).Str ( "stderr" , stderr . String ()). Msg ("Command executed" )
990975 if cmd .ProcessState .ExitCode () != 0 {
991- return out .String (), fmt .Errorf ("Command failed: " + command + " with stdout: " + out .String ())
976+ return out .String (), fmt .Errorf ("Command failed: " + command + " with stdout: " + out .String () + " stderr: " + stderr . String () )
992977 }
993978
994979 return out .String (), nil
0 commit comments