<p>I have an <code>http</code> binary file from <code>go build</code>. How do I run this binary file forever in my AWS server? I tried <code>./http &</code> but it stops running once I log out of <code>ssh</code>. How do I make sure that my binary keeps running forever? An additional feature (but not important), have it automatically run again when it crashes.</p>
<p>Kind of like Node.js's pm2 library.</p>
<hr/>**评论:**<br/><br/>Ainar-G: <pre><p>Isn't that what <a href="https://en.wikipedia.org/wiki/Init" rel="nofollow">init</a> and alike usually do?</p></pre>StabbyCutyou: <pre><p>You should use a daemonizing tool like init /upstart / systemd to handle this for you. Leaving it running in a tmux or screen session "works", but is not the correct way to handle what you want to do here.</p></pre>sjalfurstaralfur: <pre><p>yeah it's next on my todo list of things to learn</p></pre>oh-thatguy: <pre><p>SupervisorD is great for this, I also use it with node</p>
<p><a href="http://supervisord.org/" rel="nofollow">http://supervisord.org/</a></p></pre>journalctl: <pre><p>You should use <code>systemd</code> or <code>Upstart</code> if you're on an older version of Ubuntu.</p>
<p>They both have the ability to start your Go binary on boot and restart it on crash. They also log anything your program writes to Stdout or Stderr.</p></pre>din100: <pre><p>is this any good <a href="http://stackoverflow.com/questions/12486691/how-do-i-get-my-golang-web-server-to-run-in-the-background" rel="nofollow">http://stackoverflow.com/questions/12486691/how-do-i-get-my-golang-web-server-to-run-in-the-background</a> ?</p></pre>sjalfurstaralfur: <pre><p>This is perfect, thanks. I was able to get a quick bandaid solution with <code>nohup ./http &</code></p></pre>WellAdjustedOutlaw: <pre><p>Just make a service file.</p></pre>jahayhurst: <pre><p>You can nohup, or run it in a screen - both are valid solutions.</p>
<p>TBH tho, I typically like to instead write up a service script for it, and run it that way. It's not that hard to do.</p>
<p>If you're running systemd, here's an example - maybe not a good example, but an example. I may hate systemd, but at least this is simple.</p>
<pre><code>[Unit]
Description=gnosis
After=firewalld.service
Requires=firewalld.service
After=rsyslog.service
Requires=rsyslog.service
After=network.target
Requires=network.target
[Service]
User=wiki
ExecStart=/var/live/wiki-backend/webserver --config=/var/live/wiki-backend/config.json
Restart=on-failure
[Install]
WantedBy=multi-user.target
</code></pre>
<p>If you have docker, go that way, it's wonderful.</p>
<p>If you have good old sysvinit, you can get that working too. That's really not that bad either.</p></pre>cemclaug: <pre><p>screen session or a tmux session. These are really good for quick hacky solutions, just pipe the output to a log file and you're good to go.</p>
<pre><code>screen -dmS <screen_name> ./http > out.log
</code></pre></pre>mnbbrown: <pre><p>I'd suggest not using screen for running a server. Screen (and nohup) were created to maintain sessions between connections - not as an init system that can maintain dependencies, and handle restarts.</p></pre>alevinval: <pre><p>This. Screens.</p></pre>icub3d: <pre><p>I use docker for most of my go services and it works great.. There is a golang image that makes creating your own container easy. With that made, you can get docker to start it, keep it running, etc.</p></pre>
More of a Linux sysadmin question: how do I run my go http server in AWS forever (even when I ssh out)
blov · · 539 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传