How can I start go server permanently?

huangapple go评论91阅读模式
英文:

How can I start go server permanently?

问题

我有一个关于我的Go服务器的问题。

当我通过SSH连接到我的NAS并执行./gogs web时,服务器会启动。但是当我关闭SSH连接时,服务器就停止了。

我该如何永久启动我的Go服务器?

英文:

I've got problem with my Go server.

When I'm connected to my NAS via SSH and do ./gogs web, the server is starting. But when I close the SSH connection, the server is stopped.

How I can start my Go server permanently?

答案1

得分: 1

你在gogs中有一些脚本,可以让你将服务器作为守护进程启动:

这将使进程在会话关闭后仍然保持运行。

issue 172中,你还有其他选项。

英文:

You have scripts in gogs allowing you to launch the server as a daemon:

That would allow the process to remain while the session is closed.

You have other options in issues 172.

答案2

得分: 1

这不是一个特定于Go的问题,发生的情况是Go程序仍然连接到你的终端,当你退出时,内核会向仍然连接到该终端会话的每个二进制文件发送一个SIGHUP信号。

你最好的选择可能是使用nohup ./gogs web

次好的选择是重写main函数,以便拦截和处理SIGHUP信号,阻止它杀死你的程序。然而,这样做需要正确处理很多事情(你应该关闭stdin、stdout和stderr;确保所有的日志记录都通过日志库完成,...)。

英文:

This is not a Go-specifioc problem, what is happening is that the Go program is still attached to your terminal and when you log out, the kernel will trigger a SIGHUP to every binary still connected to that terminal session.

Your best option is probably to use nohup ./gogs web.

Second-best option would be to rewrite main, so that it intercepts and handles SIGHUP, stopping it from killing your program. However, doing so requires handling quite a few things properly (you really should close stdin, stdout and stderr; make sure all your logging is done through the log library, ...)

huangapple
  • 本文由 发表于 2014年11月23日 17:34:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/27087497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定