Ai
1 Star 3 Fork 2

lwow2025/snake-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
lwow2025 提交于 2018-09-26 21:46 +08:00 . fix bug
//+build darwin linux
package shell_util
import (
"gitee.com/lwow2025/snake-go/common/util"
"path/filepath"
"os"
"gopkg.in/mgo.v2/bson"
"io/ioutil"
"os/exec"
"fmt"
"gitee.com/lwow2025/snake-go/common/log"
"bufio"
"bytes"
"strings"
)
func ExecCmdByTmpFile(cmd []byte, shFileArgs ...string) (rb []byte, err error) {
tmpPath := filepath.Join(util.HomeDir(), "tmp")
os.MkdirAll(tmpPath, 0755)
tmpShFile := filepath.Join(tmpPath, bson.NewObjectId().Hex() + ".sh")
ioutil.WriteFile(tmpShFile, cmd, 0755)
defer os.RemoveAll(tmpShFile)
args := append([]string{ tmpShFile }, shFileArgs...)
//log.L.Sugar().Debug(string(cmd), " ", args)
rb, err = exec.Command("/bin/bash", args...).Output()
return
}
func Alive(appStartCmd string) bool {
rb, err := ExecCmdByTmpFile([]byte(fmt.Sprintf(`ps aux|grep "%v"`, appStartCmd)))
if err != nil {
log.L.Sugar().Error("check app alive failed", " result=", string(rb), " err=", err)
return false
}
counter := 0
scanner := bufio.NewScanner(bytes.NewReader(rb))
for scanner.Scan() {
tmpText := scanner.Text()
if strings.Contains(tmpText, appStartCmd) {
counter++
}
}
if counter > 1 {
return true
}
return false
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lwow2025/snake-go.git
git@gitee.com:lwow2025/snake-go.git
lwow2025
snake-go
snake-go
b5afa3f837d4

搜索帮助