构建Go应用程序

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

Building Go app

问题

我刚开始学习golang,并且想按照这里的说明构建一个hello world的网页。然而,当我通过SSH使用runbuild命令时,我无法“恢复”对提示符的控制。

如果我按下Ctrl + C,我的应用程序就不再工作了,我想我可能对Go的工作原理有所误解。

英文:

I just started learning golang and I want tried to build a hello world webpage as per here.However when I use run or build via SSH i don't "regain" control of the prompt.

构建Go应用程序

If I do Ctrl + C , my app no longer work , I think i am misunderstanding how Go work

答案1

得分: 1

你可以使用nohup来实现这个,像这样:

nohup ./index &

nohup用于防止用户注销后终止进程,&用于在后台启动进程。
之后你可以退出控制台。

英文:

You can use nohup for that like this:

nohup ./index &

nohup used to prevent termination of the process after user logout and & used to start process in background.
After it you can exit from console.

答案2

得分: 0

请注意,如果你使用nohup命令来运行进程,它将会一直运行,直到你使用kill <PID>命令停止它。此外,如果进程绑定到了一个HTTP端口,你需要记得停止正在运行的进程,否则你的新建/重建的进程将无法绑定到该端口。

使用一些简单的工具如Supervisor(这里有一个指南)或daemontools是更合理的管理进程的方式。下面是一个用于Go应用程序的快速Supervisor配置示例:

[program:index]
command=/home/yourappuser/bin/index
autostart=true
autorestart=true
startretries=10
# 应用程序应该以哪个用户身份运行(不要使用root用户!)
user=yourappuser
# 应用程序所在的目录
directory=/srv/www/yourapp.com/
# 环境变量
environment=APP_SETTINGS="/srv/www/yourapp.com/prod.env"
redirect_stderr=true
# 日志文件的名称
stdout_logfile=/var/log/supervisor/yourapp.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

如果你的Go应用程序不需要绑定到80端口,这个配置就可以工作。如果需要绑定到80端口,可以使用setcap命令(永远不要以root用户身份运行应用程序),例如:setcap cap_net_bind_service=+ep /home/yourappuser/bin/index

英文:

Note that if you nohup the process it will run "indefinitely" until you call kill &lt;PID&gt; on it. Further, if it binds to a HTTP port you'll need to remember to kill the running process else your new/rebuilt process won't be able to bind.

Using something simple like Supervisor (guide here) or daemontools is a more sane way to run manage processes. Here's a quick Supervisor config for a Go application:

[program:index]
command=/home/yourappuser/bin/index
autostart=true
autorestart=true
startretries=10
# the user your app should run as (i.e. *not* root!)
user=yourappuser
# where your application runs from
directory=/srv/www/yourapp.com/
# environmental variables
environment=APP_SETTINGS=&quot;/srv/www/yourapp.com/prod.env&quot;
redirect_stderr=true
# the name of the log file.
stdout_logfile=/var/log/supervisor/yourapp.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

This will work if your Go application doesn't need to bind to port 80. If it does, use setcap (and never run your application as root) with setcap cap_net_bind_service=+ep /home/yourappuser/bin/index.

huangapple
  • 本文由 发表于 2015年7月20日 23:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/31520167.html
匿名

发表评论

匿名网友

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

确定