fix: shell call bin
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"baihu/internal/database"
|
||||
"baihu/internal/logger"
|
||||
"baihu/internal/models"
|
||||
"baihu/internal/utils"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
@@ -194,12 +195,15 @@ func (s *TaskExecutionService) prepareCommand(ctx context.Context, task *models.
|
||||
if task.WorkDir != "" {
|
||||
command = fmt.Sprintf("cd %s && %s", task.WorkDir, command)
|
||||
}
|
||||
cmd = exec.CommandContext(ctx, "sh", "-c", command)
|
||||
// 使用工具函数获取合适的 shell
|
||||
shell, _ := utils.GetShell()
|
||||
cmd = exec.CommandContext(ctx, shell, "-c", command)
|
||||
}
|
||||
|
||||
// 设置环境变量
|
||||
// 设置环境变量(始终继承系统环境变量)
|
||||
cmd.Env = os.Environ()
|
||||
if len(envVars) > 0 {
|
||||
cmd.Env = append(os.Environ(), envVars...)
|
||||
cmd.Env = append(cmd.Env, envVars...)
|
||||
}
|
||||
|
||||
return cmd, nil
|
||||
|
||||
@@ -17,15 +17,16 @@ func GetShell() (shell string, args []string) {
|
||||
return envShell, []string{}
|
||||
}
|
||||
|
||||
// macOS 默认使用 zsh
|
||||
if runtime.GOOS == "darwin" {
|
||||
if _, err := exec.LookPath("/bin/zsh"); err == nil {
|
||||
return "/bin/zsh", []string{}
|
||||
// 尝试按优先级查找可用的 shell
|
||||
shells := []string{"/bin/bash", "/bin/zsh", "/bin/sh"}
|
||||
for _, sh := range shells {
|
||||
if _, err := os.Stat(sh); err == nil {
|
||||
return sh, []string{}
|
||||
}
|
||||
}
|
||||
|
||||
// Linux 默认使用 bash
|
||||
return "/bin/bash", []string{}
|
||||
// 最后回退到 sh(应该总是存在)
|
||||
return "sh", []string{}
|
||||
}
|
||||
|
||||
// GetShellCommand 返回执行命令的 shell 和参数
|
||||
|
||||
Reference in New Issue
Block a user