Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions agent/utils/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
package cmd

import (
"os"
"os/exec"
"strings"
"sync"
)

var (
bashPathOnce sync.Once
bashPathCache string
)

// BashPath returns the full path to bash, falling back to common locations
// if "bash" is not found in $PATH.
func BashPath() string {
bashPathOnce.Do(func() {
if p, err := exec.LookPath("bash"); err == nil {
bashPathCache = p
return
}
for _, candidate := range []string{"/bin/bash", "/usr/bin/bash", "/usr/local/bin/bash"} {
if _, err := os.Stat(candidate); err == nil {
bashPathCache = candidate
return
}
}
bashPathCache = "bash"
})
return bashPathCache
}

func CheckIllegal(args ...string) bool {
if args == nil {
return false
Expand Down
12 changes: 6 additions & 6 deletions agent/utils/cmd/cmdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ func (c *CommandHelper) Run(name string, arg ...string) error {
}
func (c *CommandHelper) RunBashCWithArgs(arg ...string) error {
arg = append([]string{"-c"}, arg...)
_, err := c.run("bash", arg...)
_, err := c.run(BashPath(), arg...)
return err
}

func (c *CommandHelper) RunBashC(command string) error {
if _, err := c.run("bash", "-c", command); err != nil {
if _, err := c.run(BashPath(), "-c", command); err != nil {
return err
}
return nil
}
func (c *CommandHelper) RunBashCf(command string, arg ...interface{}) error {
if _, err := c.run("bash", "-c", fmt.Sprintf(command, arg...)); err != nil {
if _, err := c.run(BashPath(), "-c", fmt.Sprintf(command, arg...)); err != nil {
return err
}
return nil
Expand All @@ -86,10 +86,10 @@ func (c *CommandHelper) RunWithStdout(name string, arg ...string) (string, error
return c.run(name, arg...)
}
func (c *CommandHelper) RunWithStdoutBashC(command string) (string, error) {
return c.run("bash", "-c", command)
return c.run(BashPath(), "-c", command)
}
func (c *CommandHelper) RunWithStdoutBashCf(command string, arg ...interface{}) (string, error) {
return c.run("bash", "-c", fmt.Sprintf(command, arg...))
return c.run(BashPath(), "-c", fmt.Sprintf(command, arg...))
}

func (c *CommandHelper) run(name string, arg ...string) (string, error) {
Expand Down Expand Up @@ -136,7 +136,7 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
cmd.Stdout = file
cmd.Stderr = file
} else if len(c.scriptPath) != 0 {
cmd = exec.Command("bash", c.scriptPath)
cmd = exec.Command(BashPath(), c.scriptPath)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
} else {
Expand Down
14 changes: 13 additions & 1 deletion core/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package i18n
import (
"embed"
"fmt"
"os"
"os/exec"
"strings"
"sync/atomic"
Expand Down Expand Up @@ -209,7 +210,18 @@ func getLanguageFromDBInternal() string {
return lang
}
func getLanguageFrom1pctl() string {
cmd := exec.Command("bash", "-c", "grep '^LANGUAGE=' /usr/local/bin/1pctl | cut -d'=' -f2")
bashBin := "bash"
if p, err := exec.LookPath("bash"); err == nil {
bashBin = p
} else {
for _, candidate := range []string{"/bin/bash", "/usr/bin/bash"} {
if _, err := os.Stat(candidate); err == nil {
bashBin = candidate
break
}
}
}
cmd := exec.Command(bashBin, "-c", "grep '^LANGUAGE=' /usr/local/bin/1pctl | cut -d'=' -f2")
stdout, err := cmd.CombinedOutput()
if err != nil {
panic(err)
Expand Down
29 changes: 28 additions & 1 deletion core/utils/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@ import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
"sync"
)

var (
bashPathOnce sync.Once
bashPathCache string
)

// BashPath returns the full path to bash, falling back to common locations
// if "bash" is not found in $PATH. This fixes issues on minimal OS installs
// (e.g., AlmaLinux) where bash may not be in the default PATH.
func BashPath() string {
bashPathOnce.Do(func() {
if p, err := exec.LookPath("bash"); err == nil {
bashPathCache = p
return
}
for _, candidate := range []string{"/bin/bash", "/usr/bin/bash", "/usr/local/bin/bash"} {
if _, err := os.Stat(candidate); err == nil {
bashPathCache = candidate
return
}
}
bashPathCache = "bash"
})
return bashPathCache
}

func SudoHandleCmd() string {
cmd := exec.Command("sudo", "-n", "ls")
if err := cmd.Run(); err == nil {
Expand All @@ -25,7 +52,7 @@ func Which(name string) bool {
}

func ExecWithStreamOutput(command string, outputCallback func(string)) error {
cmd := exec.Command("bash", "-c", command)
cmd := exec.Command(BashPath(), "-c", command)

stdout, err := cmd.StdoutPipe()
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions core/utils/cmd/cmdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,26 @@ func (c *CommandHelper) Run(name string, arg ...string) error {
}
func (c *CommandHelper) RunBashCWithArgs(arg ...string) error {
arg = append([]string{"-c"}, arg...)
_, err := c.run("bash", arg...)
_, err := c.run(BashPath(), arg...)
return err
}
func (c *CommandHelper) RunBashC(command string) error {
_, err := c.run("bash", "-c", command)
_, err := c.run(BashPath(), "-c", command)
return err
}
func (c *CommandHelper) RunBashCf(command string, arg ...interface{}) error {
_, err := c.run("bash", "-c", fmt.Sprintf(command, arg...))
_, err := c.run(BashPath(), "-c", fmt.Sprintf(command, arg...))
return err
}

func (c *CommandHelper) RunWithStdout(name string, arg ...string) (string, error) {
return c.run(name, arg...)
}
func (c *CommandHelper) RunWithStdoutBashC(command string) (string, error) {
return c.run("bash", "-c", command)
return c.run(BashPath(), "-c", command)
}
func (c *CommandHelper) RunWithStdoutBashCf(command string, arg ...interface{}) (string, error) {
return c.run("bash", "-c", fmt.Sprintf(command, arg...))
return c.run(BashPath(), "-c", fmt.Sprintf(command, arg...))
}

func (c *CommandHelper) run(name string, arg ...string) (string, error) {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
} else if len(c.scriptPath) != 0 {
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd = exec.Command("bash", c.scriptPath)
cmd = exec.Command(BashPath(), c.scriptPath)
} else {
cmd.Stdout = &stdout
cmd.Stderr = &stderr
Expand Down
3 changes: 2 additions & 1 deletion core/utils/terminal/local_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
"unsafe"

cmdutil "github.com/1Panel-dev/1Panel/core/utils/cmd"
"github.com/1Panel-dev/1Panel/core/global"
"github.com/creack/pty"
"github.com/pkg/errors"
Expand All @@ -26,7 +27,7 @@ type LocalCommand struct {
}

func NewCommand(script string) (*LocalCommand, error) {
cmd := exec.Command("bash")
cmd := exec.Command(cmdutil.BashPath())
if term := os.Getenv("TERM"); term != "" {
cmd.Env = append(os.Environ(), "TERM="+term)
} else {
Expand Down