<p>I had created a web-server file hello.go with below content:</p>
<p>package main</p>
<p>import (</p>
<pre><code>"fmt"
"net/http"
</code></pre>
<p>)</p>
<p>func helloWorld(w http.ResponseWriter, r *http.Request){</p>
<pre><code>fmt.Fprintf(w, "Hello World")
</code></pre>
<p>}</p>
<p>func main() {</p>
<pre><code>http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8080", nil)
</code></pre>
<p>}</p>
<p>When i am running as go run hello.go from putty.</p>
<p>I am able to access server is running fine and able to access <a href="http://93.188.165.37:8080/" rel="nofollow">http://93.188.165.37:8080/</a>.</p>
<p><em>How we can run web-server on background process?</em></p>
<p><strong>When I can close the putty session or after logged out from putty I m unable to access to my web-server.</strong></p>
<hr/>**评论:**<br/><br/>partkyle: <pre><p><code>go run</code> is useful for running things quickly, but it isn't the best method for deploying an application. You will want to run <code>go build</code> to create an executable file that you will run. This way, you won't have to recompile everytime you run your program. You will have to run <code>go build</code> every time there is a code change. </p>
<p>As for keeping a process running, for a real production app you will want to look in to using an init system. Assuming you are using CentOS 7, that would be systemd by default. </p>
<p>If you don't want to go that far, look in to using screen or tmux to keep it running in a shell when you log out. </p>
<p>Sorry for the brevity of my reply and my lack of links, I'm ok mobile now. All of this should be easily searchable on Google. </p>
<p>Happy coding!</p></pre>Cabraca: <pre><p><a href="https://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html" rel="nofollow">https://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html</a></p>
<p>This should do the trick</p></pre>Gambletron: <pre><p>You can also put Linux processes on a background thread by ending the command with '&'. The caveat is that the process is still part of you putty shell, so when you close the shell it will close. And the useful command is 'nohup' (no-hang-up)</p>
<p>So if you're looking to boot it up one-time (<a href="/u/partkyle" rel="nofollow">/u/partkyle</a> mentioned adding it to init.d, which is the permanent solution), you can either <code>nohup git run &</code> or build the exe and <code>nohup ./server &</code></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传