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

lihainiao · · 2665 次点击
os/exec,不是调的操作系统自身bash,直接启动第三方程序,没有独立进程(无法脱离go独自运行)。只能调用第三方程序。[]string{""}可以传空切片。
#5
更多评论
调用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