func runCmd()

in main/cmds.go [256:303]


func runCmd(ctx log.Logger, dir string, cfg handlerSettings) (err error) {
	ctx.Log("event", "executing command", "output", dir)
	var cmd string
	var scenario string
	var scenarioInfo string

	// So many ways to execute a command!
	if cfg.publicSettings.CommandToExecute != "" {
		ctx.Log("event", "executing public commandToExecute", "output", dir)
		cmd = cfg.publicSettings.CommandToExecute
		scenario = "public-commandToExecute"
	} else if cfg.protectedSettings.CommandToExecute != "" {
		ctx.Log("event", "executing protected commandToExecute", "output", dir)
		cmd = cfg.protectedSettings.CommandToExecute
		scenario = "protected-commandToExecute"
	} else if cfg.publicSettings.Script != "" {
		ctx.Log("event", "executing public script", "output", dir)
		if cmd, scenarioInfo, err = writeTempScript(cfg.publicSettings.Script, dir, cfg.publicSettings.SkipDos2Unix); err != nil {
			return
		}
		scenario = fmt.Sprintf("public-script;%s", scenarioInfo)
	} else if cfg.protectedSettings.Script != "" {
		ctx.Log("event", "executing protected script", "output", dir)
		if cmd, scenarioInfo, err = writeTempScript(cfg.protectedSettings.Script, dir, cfg.publicSettings.SkipDos2Unix); err != nil {
			return
		}
		scenario = fmt.Sprintf("protected-script;%s", scenarioInfo)
	}

	// Store the active process id and start time in case second instance was started by the agent
	// If process exited successfully the pid file is deleted
	SaveCurrentPidAndStartTime(pidFilePath)
	defer DeleteCurrentPidAndStartTime(pidFilePath)

	begin := time.Now()
	err = ExecCmdInDir(cmd, dir)
	elapsed := time.Now().Sub(begin)
	isSuccess := err == nil

	telemetry("scenario", scenario, isSuccess, elapsed)

	if err != nil {
		ctx.Log("event", "failed to execute command", "error", err, "output", dir)
		return errors.Wrap(err, "failed to execute command")
	}
	ctx.Log("event", "executed command", "output", dir)
	return nil
}