Linux 如何让我的 Golang Web 服务器在后台运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12486691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:23:47  来源:igfitidea点击:

How do I get my Golang web server to run in the background?

linuxshelldaemon

提问by quakkels

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+Zthen 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 上运行它。谢谢你的帮助。

采纳答案by nemo

Simple / Usable things first

简单/可用的东西优先

If you want a start script without much effort, you could use the upstartservice. See the corresponding manual page and /etc/init/*.conffor 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 nohupcommand, which discards the HANGUP signal.

&告诉shell在后台启动命令,保持它在任务列表。在某些 shell 上,如果父 shell 使用 HANGUP 信号退出,则作业将被终止。为了防止这种情况,您可以使用nohup丢弃 HANGUP 信号的命令来启动您的命令。

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

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

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 的作业列表中删除该进程。对于两个众所周知的 shell,这可以通过以下方式实现:

bash:

重击:

./myexecutable &
disown <pid>

zsh:

zsh:

./myexecutable &!

Killing your background job

杀死你的后台工作

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

通常,shell 会打印进程的 PID,然后可以使用kill命令将其杀死,以停止服务器。如果您的外壳不打印 PID,您可以使用

echo $!

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

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

回答by 3on

You could use Supervisordto manage your process.

您可以使用Supervisord来管理您的流程。

回答by lunixbochs

Ubuntu? Use upstart.

乌班图?使用upstart.

Create a file in /etc/initfor 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 your-service-name, 以及:stop, restart,status

回答by Greg

This will configure your service using systemd, not a comprehensive tutorial but rather a quick jump-start of how this can be set up.

这将使用 配置您的服务systemd,而不是一个全面的教程,而是如何设置的快速入门。

Content of your app.servicefile

你的app.service文件内容

[Unit]  
Description=deploy-webhook service
After=network.target

[Service]      
ExecStart=/usr/bin/go webhook.go    
WorkingDirectory=/etc/deploy-webhook

User=app-svc      
Group=app-svc

Restart=always    
RestartSec=10    
KillSignal=SIGINT

SyslogIdentifier=deploy-webhook-service      
PrivateTmp=true  

Environment=APP_PARAM_1=ParamA
Environment=APP_PARAM_2=ParamB

[Install]      
WantedBy=multi-user.target  

Starting the Service

启动服务

sudo systemctl start deploy-webhook.service

Service Status

服务状态

sudo systemctl status deploy-webhook.service

Logs

日志

journalctl -u deploy-webhook -e

回答by Roee Gavirel

After you press ctrl+z(putting the current task to sleep) you can run the command bgin the terminal (stands for background) to let the latest task continue running in the background.

ctrl+ z(使当前任务进入睡眠状态)后,您可以bg在终端(代表后台)中运行该命令,让最新的任务继续在后台运行。

When you need to, run fgto get back to the task.

需要时,运行fg以返回任务。

To get the same result, you can add to your command &at the end to start it in the background.

要获得相同的结果,您可以在命令&末尾添加以在后台启动它。