我如何让Golang Web服务器在后台运行? [英] How do I get my Golang web server to run in the background?

查看:1668
本文介绍了我如何让Golang Web服务器在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近完成了 Wiki网页开发教程 http:/ /golang.org/doc/articles/wiki/ )。我有很多的乐趣,我想用net / http包进行更多的实验。

然而,我注意到当我从控制台运行wiki时,wiki接管控制台。如果我关闭控制台终端或使用 CTRL + Z 停止进程,则服务器停止。



如何让服务器运行的背景?我认为这个术语是在一个守护进程中运行的。



我在Ubuntu 12.04上运行它。感谢您的帮助。

解决方案

简单/可用的东西首先



如果您不需要太多努力就可以启动脚本,则可以使用 upstart 服务。有关示例,请参见相应的手册页和 / etc / init / *。conf 。创建这样一个过程
后,您可以通过调用

  service myserver start 

如果您需要更多功能,例如特定限制或权限管理,您可以尝试 xinetd

使用shell



您可以像这样开始您的过程:

  nohup ./myexecutable& 

& 指示shell启动该命令在后台,将其保存在作业列表中。
在某些shell上,如果父shell使用HANGUP信号退出,则作业会被终止。
为防止发生这种情况,您可以使用放弃HANGUP信号的 nohup 命令启动您的命令。



<但是,如果被调用的进程重新连接HANGUP信号,这不起作用。



要确定,您需要从shell的工作列表中删除进程。
对于两个众所周知的炮弹,这可以通过以下方式实现:

bash:



  ./ myexecutable& 
disown< pid>



zsh:



  ./ myexecutable&! 



杀死你的后台作业



通常, shell打印进程的PID,然后可以使用 kill 命令来终止该进程的PID以停止服务器。如果您的shell不打印PID,您可以使用

  echo $! 

直接执行后。这将打印分叉进程的PID。


I have recently completed the Wiki web development tutorial (http://golang.org/doc/articles/wiki/). I had tons of fun and I would like to experiment more with the net/http package.

However, I noticed that when I run the wiki from a console, the wiki takes over the console. If I close the console terminal or stop the process with CTRL+Z then the server stops.

How can I get the server to run in the background? I think the term for that is running in a daemon.

I'm running this on Ubuntu 12.04. Thanks for any help.

解决方案

Simple / Usable things first

If you want a start script without much effort, you could use the upstart service. See the corresponding manual page and /etc/init/*.conf for examples. After creating such a process you can start your server by calling

service myserver start

If you want more features, like specific limitations or permission management, you could try xinetd.

Using the shell

You could start your process like this:

nohup ./myexecutable &

The & tells the shell to start the command in the background, keeping it in the job list. On some shells, the job is killed if the parent shell exits using the HANGUP signal. To prevent this, you can launch your command using the nohup command, which discards the HANGUP signal.

However, this does not work, if the called process reconnects the HANGUP signal.

To be really sure, you need to remove the process from the shell's joblist. For two well known shells this can be achieved as follows:

bash:

./myexecutable &
disown <pid>

zsh:

./myexecutable &!

Killing your background job

Normally, the shell prints the PID of the process, which then can be killed using the kill command, to stop the server. If your shell does not print the PID, you can get it using

echo $!

directly after execution. This prints the PID of the forked process.

这篇关于我如何让Golang Web服务器在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆