How to run a go app continously on an Ubuntu server

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

How to run a go app continously on an Ubuntu server

问题

在这里似乎找不到直接的答案。

我不确定是否应该将"./myBinary"作为Cron进程运行,还是应该运行"go run myapp.go"。

确保它始终运行的有效方法是什么?
抱歉,我习惯于使用Apache和Nginx。

此外,部署Go应用的最佳实践是什么?我希望所有内容(最好)都在同一台服务器上提供,就像我的开发环境一样。

我读到另一篇文章中提到了使用S3,但是我真的不想使用S3。

英文:

Couldn't seem to find a direct answer around here.

I'm not sure if I should run ./myBinary as a Cron process or if I should run "go run myapp.go"

What's an effective way to make sure that it is always running?
Sorry I'm used to Apache and Nginx.

Also what are best practices for deploying a Go app? I want everything (preferably) all served on the same server. Just like how my development environment is like.

I read something else that used S3, but, I really don't want to use S3.

答案1

得分: 5

使用您的初始进程提供的功能。您可能正在运行带有Systemd或Upstart的系统。它们都有非常简单的服务描述,并且可以确保您的应用以正确的权限运行,当任何事情发生故障时重新启动,并正确处理输出。

对于快速的Upstart描述,请点击这里,您的服务描述可能只需如下:

start on runlevel [2345]
stop on runlevel [!2345]
setuid 您的应用程序运行的用户名
exec /path/to/your/app --options

对于快速的Systemd描述,请点击这里,您的服务可能只需如下:

[Unit]
Description=您的服务

[Service]
User=您的应用程序运行的用户名
ExecStart=/path/to/your/app --options

[Install]
WantedBy=multi-user.target
英文:

Use the capabilities your init process provides. You're likely running system with either Systemd or Upstart. They've both got really easy descriptions of services and can ensure your app runs with the right privileges, is restarted when anything goes down, and that the output is are handled correctly.

For quick Upstart description look here, your service description is likely to be just:

start on runlevel [2345]
stop on runlevel [!2345]
setuid the_username_your_app_runs_as
exec /path/to/your/app --options

For quick Systemd description look here, your service is likely to be just:

[Unit]
Description=Your service

[Service]
User=the_username_your_app_runs_as
ExecStart=/path/to/your/app --options

[Install]
WantedBy=multi-user.target

答案2

得分: 0

你可以将它放在一个无限循环中,例如:

#! /bin/sh
while true; do
  go run myapp.go
  sleep 2 # 以防万一
done

因此,一旦应用由于某种原因停止运行,它将会再次运行。

你可以将它放在一个脚本中,并在后台运行:

$ nohup ./my-script.sh >/dev/null 2>&1 &
英文:

You can put it in an inifiny loop, such as:

#! /bin/sh
while true; do
  go run myapp.go
  sleep 2 # Just in case
done

Hence, once the app dies due some reason, it will be run again.

You can put it in a script and run it in background using:

$ nohup ./my-script.sh >/dev/null 2>&1 &

答案3

得分: 0

你可能想要使用类似screen的虚拟终端工具。示例:

screen -S myapp # 创建名为myapp的screen
cd ... # 切换到你的应用程序目录
go run myapp.go # 或者使用go install,然后从go bin目录运行./myapp
Ctrl-a+d # 退出screen

如果你想返回到screen:

screen -r myapp

编辑:这个解决方案将在你退出终端时保持进程,但如果进程崩溃,它不会重新启动。

英文:

You may want to go for virtual terminal utility like screen here. Example:

screen -S myapp # create screen with name myapp
cd ... # to your app directory
go run myapp.go # or go install and then ./myappfrom go bin dir
Ctrl-a+d # to go out of screen

If you want to return to the screen:

screen -r myapp

EDIT: this solution will persist the process when you go out of terminal, but won't restart it when it'll crash.

huangapple
  • 本文由 发表于 2015年7月24日 12:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/31602094.html
匿名

发表评论

匿名网友

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

确定