如何让Golang web服务器在后台运行?

时间:2022-02-14 20:53:07

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.

我最近完成了Wiki web开发教程(http://golang.org/doc/articles/wiki/)。我得到了很多乐趣,我想尝试更多的net/http包。

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.

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

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.

我在Ubuntu 12.04上运行这个。感谢任何帮助。

3 个解决方案

#1


53  

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

如果您想要一个不费多大力气的开始脚本,您可以使用upstart服务。参见相应的手册页和/etc/init/*。conf的例子。创建这样的进程之后,可以通过调用启动服务器

service myserver start

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

如果您想要更多的特性,比如特定的限制或权限管理,您可以尝试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.

&告诉shell在后台启动命令,并将其保存在作业列表中。在某些shell中,如果父shell使用挂起信号退出,则作业将被终止。为了防止这种情况发生,您可以使用nohup命令来启动命令,该命令丢弃了挂起信号。

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:

要真正确定,您需要从shell的joblist中删除进程。对于两种已知的壳层,可以实现以下目标:

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

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

echo $!

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

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

#2


9  

You could use Supervisord to manage your process.

您可以使用监理来管理您的过程。

#3


6  

Ubuntu? Use upstart.

Ubuntu吗?使用新贵。

Create a file in /etc/init for your job, named your-service-name.conf

在/etc/init中为作业创建一个文件,命名为your-service-name.conf

start on net-device-up
exec /path/to/file --option

You can use start your-service-name, as well as: stop, restart, status

您可以使用start -service-name以及:stop、restart、status

#1


53  

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

如果您想要一个不费多大力气的开始脚本,您可以使用upstart服务。参见相应的手册页和/etc/init/*。conf的例子。创建这样的进程之后,可以通过调用启动服务器

service myserver start

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

如果您想要更多的特性,比如特定的限制或权限管理,您可以尝试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.

&告诉shell在后台启动命令,并将其保存在作业列表中。在某些shell中,如果父shell使用挂起信号退出,则作业将被终止。为了防止这种情况发生,您可以使用nohup命令来启动命令,该命令丢弃了挂起信号。

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:

要真正确定,您需要从shell的joblist中删除进程。对于两种已知的壳层,可以实现以下目标:

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

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

echo $!

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

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

#2


9  

You could use Supervisord to manage your process.

您可以使用监理来管理您的过程。

#3


6  

Ubuntu? Use upstart.

Ubuntu吗?使用新贵。

Create a file in /etc/init for your job, named your-service-name.conf

在/etc/init中为作业创建一个文件,命名为your-service-name.conf

start on net-device-up
exec /path/to/file --option

You can use start your-service-name, as well as: stop, restart, status

您可以使用start -service-name以及:stop、restart、status