// CheckServices checks if a service is running on the host
func CheckServices(ctx context.Context, e ctxt.Executor, host, service string, disable bool) *CheckResult {
result := &CheckResult{
Name: CheckNameSysService,
}
// check if the service exist before checking its status, ignore when non-exist
stdout, _, err := e.Execute(
ctx,
fmt.Sprintf(
"systemctl list-unit-files --type service | grep -i %s.service | wc -l", service),
true)
if err != nil {
result.Err = err
return result
}
if cnt, _ := strconv.Atoi(strings.Trim(string(stdout), "\
")); cnt == 0 {
if !disable {
result.Err = fmt.Errorf("service %s not found, should be installed and started", service)
}
result.Msg = fmt.Sprintf("service %s not found, ignore", service)
return result
}
active, err := GetServiceStatus(ctx, e, service+".service")
if err != nil {
result.Err = err
}
switch disable {
case false:
if !strings.Contains(active, "running") {
result.Err = fmt.Errorf("service %s is not running", service)
result.Msg = fmt.Sprintf("start %s.service", service)
}
case true:
if strings.Contains(active, "running") {
result.Err = fmt.Errorf("service %s is running but should be stopped", service)
result.Msg = fmt.Sprintf("stop %s.service", service)
}
}
return result
}
https://github.com/pingcap/tiup/blob/master/pkg/cluster/operation/check.go