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

lihainiao · 2023-05-19 09:10:14 · 3035 次点击

可行归可行,里面各种参数和分支处理起来很累人。用os/exec好了。 还有就是>>>等只是shell自己的语法,脱离了shell环境就不能再用了。

#4
更多评论

调用python脚本start.py

#!/usr/bin/python
import os
os.system("nohup /home/fpz/git/ywpt/go-client/ywpt_client_upgrade/ZhengheMonitorClient-linux-0.1.0 >/dev/null &")
startfile := "start.py"
err5 := syscall.Exec(startfile, []string{">/dev/null"}, os.Environ())

这种方式会有输出直接导致go主进程结束 image.png

#1

syscall.Exec会直接在当前进程执行,

改用os.StartProcess(startfile, []string{""}, &os.ProcAttr{})可行

#2