Files
msw/shell/shell.go
2024-05-16 15:51:06 +08:00

47 lines
956 B
Go

package shell
import (
"log"
"os"
"os/exec"
"path/filepath"
)
func ExecuteOne(name string, args ...string) {
KillAll(name)
Execute(name, args...)
}
func Execute(name string, args ...string) {
// get the directory of the executable
file, err := os.Executable()
if err != nil {
log.Fatal("Error getting executable path:", err)
}
dir := filepath.Dir(file)
// change the current working directory to the directory of the executable
err = os.Chdir(dir)
if err != nil {
log.Fatal("Error changing working directory:", err)
}
log.Println("Executing command:", name, args)
cmd := exec.Command(name, args...)
// cmd.Stderr = os.Stderr
// cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
log.Println("Error executing command:", name, args, err)
}
}
func KillAll(name string) {
cmd := exec.Command("pkill", "-f", name)
err := cmd.Run()
if err != nil {
log.Println("Error killing all processes with name:", name, err)
}
}