保持Go服务器作为后台进程运行

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

Keep running Go Server as background process

问题

我想要无论是否发生错误,都能保持我的 Golang 语言 Web 服务器运行。如何让它始终保持运行?

英文:

I want to keep my Golang language web server working regardless of an error happens or not.
How to keep running it always?

答案1

得分: 14

我们需要从两个角度检查一个始终运行的服务器:

  1. 处理/容忍在处理请求时发生的错误。
  2. 如果服务器应用程序崩溃或被终止,重新启动服务器应用程序。

对于第一个问题,你不需要做任何特殊处理。如果你的处理程序发生恐慌,它不会导致整个服务器崩溃,HTTP服务器将从中恢复。它只会停止处理该特定请求。当然,你可以创建自己的处理程序,调用其他处理程序,并在恐慌时进行恢复,并以智能的方式处理它,但这不是必需的。

这里需要注意的一点是:Go应用程序在main goroutine结束时结束(即:main()函数返回)。因此,即使处理请求的goroutine受到保护,如果主goroutine结束(例如发生恐慌),你的应用程序仍然会退出。

对于第二个问题,这与Go无关。例如,如果你在Linux上,只需将编译后的Go可执行文件安装/注册为服务,并正确配置以在退出或崩溃时重新启动。

例如,在使用systemd进行服务配置的Ubuntu中,以下最小化的服务描述符将满足你的要求:

[Unit]
Description=My Always-on Service

[Service]
Restart=always
Type=simple
ExecStart=/path/to/your/app -some=params passed

[Install]
WantedBy=multi-user.target

将上述文本放入文件中,例如/etc/systemd/system/myservice.service,然后可以使用以下命令启用和启动它:

sudo systemctl enable myservice.service 
sudo systemctl start myservice.service 

第一条命令会在正确的文件夹中创建一个符号链接,以便在系统启动时自动启动它。第二条命令立即启动它。

要验证它是否正在运行,请键入:

sudo systemctl status myservice.service 

(在大多数情况下,你可以省略.service扩展名。)

现在,无论你的应用程序崩溃还是操作系统重新启动,你的应用程序都将自动启动/重新启动。

有关systemd的进一步阅读和教程:

如何使用Systemctl管理Systemd服务和单元

Systemd基础:使用服务、单元和日志

将“在启动时运行”脚本从Upstart转换为Ubuntu 16的Systemd

Systemd:编写和启用服务

英文:

We have to inspect an always-on server from 2 perspectives:

  1. Handle / tolerate errors that occur during serving requests
  2. Restart the server app if it crashes or gets killed

For the first, you don't have to do anything special. If your handler panics, it will not blow your whole server, the http server will recover from that. It will only stop serving that specific request. Of course you can create your own handler which calls other handlers and recovers on panic and handle it in an intelligent way, but this is not a requirement.

<sup>One thing to note here: a Go app ends when the main goroutine ends (that is: the main() function returns). So even though goroutines serving requests are protected, if your main goroutine would end (e.g.
panic), your app would still exit.</sup>

For the second, it's not really Go related. For example if you're on linux, simply install / register your compiled Go executable as a service, properly configured to have it restarted should it exit or crash.

For example in Ubuntu which uses systemd for service configuration, the following minimal service descriptor would fulfill your wishes:

[Unit]
Description=My Always-on Service

[Service]
Restart=always
Type=simple
ExecStart=/path/to/your/app -some=params passed

[Install]
WantedBy=multi-user.target

Put the above text in a file e.g. /etc/systemd/system/myservice.service, and you can enable and start it with:

sudo systemctl enable myservice.service 
sudo systemctl start myservice.service 

The first command places a symlink to the proper folder to have it auto-started on system startup. The second command starts it right now.

To verify if it's running, type:

sudo systemctl status myservice.service 

(You can omit the .service extension in most cases.)

Now whenever your app crashes, or the OS gets restarted, your app will be automatically started / restarted.

Further reading and tutorials for systemd:

How To Use Systemctl to Manage Systemd Services and Units

Systemd Essentials: Working with Services, Units, and the Journal

Convert "run at startup" script from upstart to systemd for Ubuntu 16

systemd: Writing and Enabling a Service

答案2

得分: 1

有几件事情你可以做。比如,

  1. 如果你担心程序崩溃,可以将服务器作为后台进程运行。
    user@terminal# go run server.go &
  2. 否则,对于服务器崩溃的情况,你可以编写Shell脚本来重新运行它并杀死旧的进程。
英文:

There are few things which you can do. Such as,

  1. If you are worrying about panics, the simply run the server as a background process.
    user@terminal# go run server.go &
  2. Otherwise, for server crashes, you may write Shell script to re-run it and kill the old process.

huangapple
  • 本文由 发表于 2017年4月27日 14:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/43650183.html
匿名

发表评论

匿名网友

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

确定