How to detach the Gin webserver from the terminal?

polaris · · 739 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey <a href="/r/golang" rel="nofollow">/r/golang</a>!</p> <p>I&#39;m trying to deploy a quick web server I wrote. Everything works fine when I run it from the command line, but I&#39;d like to be able to log out of my SSH session and still have the Gin app running.</p> <p>Could anyone guide me to a solution?</p> <hr/>**评论:**<br/><br/>postman_: <pre><p>screen/nohup/supervisord/thousands of other options</p></pre>alaskacodes: <pre><p>Don&#39;t use screen/tmux/nohup outside of development. :( If you want this to be production or semi-production, talk to ops (if it&#39;s not you), or use supervisord/upstart/systemd or deploy to your container or cloud platform of choice.</p></pre>knrz: <pre><p>Why not screen/tmux/nohup? /Independent dev having his own fun, so no ops team :)</p></pre>alaskacodes: <pre><p>Well, note that I said &#34;outside of development&#34;. If you&#39;re developing, tmux away, buddy! (still don&#39;t use nohup, though. nohup sucks)</p> <p>If you&#39;re deploying something when you&#39;re not actively developing, all three methods mentioned above won&#39;t persist across a server reboot and in general are kind of a kludgy way to keep a process going.</p> <p>What would you rather do for deployment?</p> <p><strong>tmux</strong></p> <pre><code>ssh me@mybox tmux attach -t &lt;session name (provided you gave it a meaningful one)&gt; ctrl-c ./mything &gt; /var/log/mything.log 2&gt;&amp;1 ctrl-b n ctrl-c tail -f /var/log/mything.log (or look at syslog, depending) crtl-b $ &lt;and give it a nice name if you haven&#39;t&gt; ctrl-b d </code></pre> <p>And then have to reattach to check logs and whatnot, or:</p> <p><strong>nohup</strong></p> <pre><code>ssh me@mybox ps (find my job&#39;s pid) kill &lt;job&#39;s pid&gt; cd /wherever/my/bin/is nohup ./mything &gt; /var/log/mything.log 2&gt;&amp;1 &amp; tail -f /var/log/mything.log </code></pre> <p>As opposed to:</p> <p><strong>upstart</strong></p> <pre><code>ssh me@mybox service mythingd &lt;start|stop|restart&gt; tail -f /var/log/mythingd.log </code></pre> <p>Or:</p> <p><strong>supervisord</strong></p> <pre><code>ssh me@mybox supervisorctl restart mythingd tail -f /var/log/mything.log </code></pre> <p>The latter two are just so much more convenient, IMHO, and much easier to transform into a deployable package, if you decide to go that route, because you&#39;ve already done a lot of the footwork.</p> <p>Plus, correct me someone if I&#39;m wrong, but once you&#39;ve run something with nohup, it does exactly that: ignore SIGHUP. So, if you&#39;ve decided to get all fancy and provide SIGHUP-driven config reloading, you lose that. Small niggle, I know, and you always have like SIGUSR1, but it&#39;s one less signal you can use.</p></pre>010a: <pre><p><a href="http://supervisord.org/" rel="nofollow">Supervisor</a> is my favorite. </p></pre>strothjs: <pre><p>You can use your operating system&#39;s service manager to configure your app to start. Ubuntu uses Upstart or systemd.</p> <p>You can install supervisord which can manage your service for you.</p> <p>Other option is to detach the service from your she&#39;ll session. Common tool for that is screen.</p></pre>ecmdome: <pre><p>People mentioned screen which is a good choice.</p> <p>Have you considered deploying using docker? </p></pre>mtanski: <pre><p>upstart / systemd can manage the process for you. and you get rotated log output in /var/log by default.</p></pre>anoland: <pre><p>Uhm... what do you need gin for after deployment? I use gin so that I get the log output for debugging and to recompile files after saving. But after I deploy I wouldn&#39;t use it. Just run the program.</p></pre>tofufish: <pre><p>I think he is referring to <a href="https://github.com/codegangsta/gin" rel="nofollow">https://github.com/codegangsta/gin</a> and not <a href="https://github.com/gin-gonic/gin" rel="nofollow">https://github.com/gin-gonic/gin</a></p></pre>alaskacodes: <pre><p>he (anoland) sounds like he&#39;s referring to codegangsta/gin and OP is indeed probably referring to gin-gonic/gin. Just clarifying, since your order above is not what I would expect.</p></pre>ZenSwordArts: <pre><p>If you decide to use something like screen have a look at <a href="http://byobu.co/" rel="nofollow">Byobu</a>. It should be in your distros repositories.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

739 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传