如何用Go启动一个外部程序(linux平台)

lihainiao · · 2665 次点击
调用python脚本start.py ```python #!/usr/bin/python import os os.system("nohup /home/fpz/git/ywpt/go-client/ywpt_client_upgrade/ZhengheMonitorClient-linux-0.1.0 >/dev/null &") ``` ```go startfile := "start.py" err5 := syscall.Exec(startfile, []string{">/dev/null"}, os.Environ()) ``` 这种方式会有输出直接导致go主进程结束 ![image.png](https://static.golangjob.cn/230519/f19e0dedcb2fc5eb1bb80659106ce988.png)
#1
更多评论
syscall.Exec会直接在当前进程执行, 改用os.StartProcess(startfile, []string{""}, &os.ProcAttr{})可行
#2
是不是参数比较复杂,参数不复杂的时候用 ```go cmd := exec.Command("reboot") _, err = cmd.Output() ``` 复杂的时候可以用 ```go shell := "xxxxxxx" cmd := exec.Command("/bin/sh", "-c", shell) _, err = cmd.Output() ``` 我感觉用复杂参数带符号的时候容一出毛病。
#3