请教下 Linux 下 golang 编译的二进制程序开机启动

2020-06-24 10:52:16 +08:00
 la0wei
请教下 linux 下 golang 编译的二进制程序开机启动

背景:
需要一个伪装站,最好是视频类的,流量大点,所以想到用 golang 的程序,一般单文件,对我这种 linux 苦手再好不过。
找到这么几个
a https://github.com/wybiral/tube
b https://github.com/prologic/tube 从上一个 fork
c https://github.com/stashapp/stash

现象:
a 程序终端正常运行,rc.localnohup&启动无法运行,打印信息后看到
panic: html/template: pattern matches no files: `templates/*`

goroutine 1 [running]:
html/template.Must(...)
/home/davy/.go/src/html/template/template.go:372
github.com/wybiral/tube/pkg/app.NewApp(0xc000065290, 0xc0000652f0, 0x1, 0x8b8360)
/home/davy/go/src/github.com/wybiral/tube/pkg/app/app.go:54 +0xf06
main.main()
/home/davy/go/src/github.com/wybiral/tube/main.go:17 +0x224
panic: html/template: pattern matches no files: `templates/*`
上面的路径非我本机路径,应该是开发者的

b 程序终端正常运行,单文件,rc.localnohup&记不太清了,似乎是可以的

c 程序终端正常运行,单文件,rc.localnohup&启动无法运行,打印信息后看到
panic: Config File "config" Not Found in "[/ /.stash]"

goroutine 1 [running]:
github.com/stashapp/stash/pkg/manager.initConfig()
/stash/pkg/manager/manager.go:74 +0x2e6
github.com/stashapp/stash/pkg/manager.Initialize.func1()
/stash/pkg/manager/manager.go:43 +0x35
sync.(*Once).Do(0x274f2d8, 0x1cc6c38)
/usr/local/go/src/sync/once.go:44 +0xb3
github.com/stashapp/stash/pkg/manager.Initialize(0xc0001d9f88)
/stash/pkg/manager/manager.go:40 +0x39
main.main()
/stash/main.go:15 +0x22

疑问:rc.local 启动程序和终端启动有啥区别,为何程序启动后有不同的输出?如何开机启动?
4076 次点击
所在节点    Go 编程语言
31 条回复
kuro1
2020-06-24 11:03:12 +08:00
工作路径不同
GeruzoniAnsasu
2020-06-24 11:07:45 +08:00
https://github.com/stashapp/stash/blob/develop/docker/build/x86_64/Dockerfile

这不是有 docker 吗
开机把容器拉起来就好了,有这么纠结?
Tink
2020-06-24 11:39:11 +08:00
完整路径
la0wei
2020-06-24 11:53:04 +08:00
@kuro1 确实怀疑路径不同,但是我用的绝对路径
@GeruzoniAnsasu docker 编写 dockerfile 还是太麻烦了,go 在 docker 里似乎还有些小 trick,比如 gosu 什么的,不愿意搞
@Tink 使用的绝对路径
RubyJack
2020-06-24 11:54:02 +08:00
装一个 supervisor 写一个配置文件搞定
no1xsyzy
2020-06-24 12:38:05 +08:00
@la0wei #4 可能 pwd 不同导致程序行为不同,或者 uid 不同,或者其他什么什么什么。
建议不要 nohup 而是采用 daemonizer,比如 screen supervisor systemd
zjsxwc
2020-06-24 13:11:12 +08:00
除了 supervisor
还可以 crontab @reboot
kiracyan
2020-06-24 14:11:44 +08:00
systemd
vinsec
2020-06-24 14:25:35 +08:00
用 systemd 守护 资源文件路径用绝对路径
la0wei
2020-06-24 15:56:38 +08:00
@RubyJack @no1xsyzy
抱歉,工作走开很久。刚刚试了下,supervisor 确实简单好用,不过又遇到问题了。
补充一点,stash 这个程序会占据终端输出,所以要放在后台。然而使用 supervisor 启动后,程序不停的启动退出,pid 一直在变,这有可能是啥原因

@zjsxwc crontab 不知道能否把程序放在后台运行,supervisor 遇到程序一直启动退出的循环问题

@kiracyan @vinsec systemd 要写 service 文件,还不确定能成功,我太懒了,想先把 supervisor 搞定再说
54007
2020-06-24 16:13:20 +08:00
我这里有一个例子吧 golang 程序注册成服务,开机启动 https://github.com/hellojukay/systemd-demo
fanpei0121
2020-06-24 16:43:24 +08:00
pm2 管理进程
chenqh
2020-06-24 16:43:47 +08:00
@la0wei 你这程序不能前台运行?
kiracyan
2020-06-24 17:02:06 +08:00
@la0wei 其实 supervisor 是用 systemd 来管理的 启动退出可能是你程序崩了触发了重启机制
la0wei
2020-06-24 17:28:36 +08:00
@54007 谢谢,晚上试试看
@fanpei0121 谢谢,这个没有试过,先尝试已掌握的知识上解决
@chenqh 谢谢,正是因为前台运行,会占用终端,在终端输出内容。我想转到后台,不用一直占着终端,以前我都是用 tmux 来解决的,但是不能重启,而且 ssh 你也知道这网络情况,上去一次真不容易
ruanimal
2020-06-24 17:51:00 +08:00
@la0wei 你用 supervisor 就不会占用终端了, 用 supervisor 的程序不要以 daemon 模式运行,直接前台启动就好了
keepeye
2020-06-24 18:10:07 +08:00
systemd 最优
supervisor 次之
其他的不建议
caola
2020-06-24 18:16:19 +08:00
systemd 最好,
默认的 supervisor 进程还得靠 systemd 来守护。。。。
no1xsyzy
2020-06-24 19:46:12 +08:00
@la0wei #10 我试了下 wybiral/tube 在 screen 下和 supervisor 下都正常……
具体你应该 supervisorctl 进去用 tail 、tail <program> stderr 之类看看 log 。
la0wei
2020-06-24 20:06:24 +08:00
@ruanimal 我也是这个想法,但是没有成功,一个参数都没敢加,如果我的配置文件没错的话,效果和终端运行应该是差不多的,奈何总是退出

@keepeye systemd 总是要学的,现在偷懒,想先把服务跑起来,以后再完善些

@caola supervisor 还是要简单一点的

@no1xsyzy 我的 vps 用的 root,supervisor 配置文件如下
[program:stash]
command=/root/stash/stash-linux
directory=/root/stash/
user=root
numprocs=1
screen 一定是没问题的。以前都用 screen 或 tmux 来运行 golang 的程序,现在想要进步一点

tail -100 stash 后提示
Warning: sys.stdout.encoding is set to iso8859-1, so Unicode output may fail. Check your LANG and PYTHONIOENCODING environment settings.

正在搜索解决方案

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/684369

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX