我最近完成了Wiki Web开发教程(http://golang.org/doc/articles/wiki/).我有很多乐趣,我想尝试更多的net/http包.
但是,我注意到当我从控制台运行wiki时,wiki会接管控制台.如果我关闭控制台终端或停止进程,CTRL+Z则服务器停止.
如何让服务器在后台运行?我认为这个术语是在一个守护进程中运行的.
我在Ubuntu 12.04上运行它.谢谢你的帮助.
nem*_*emo 57
如果您想要一个没有太多努力的启动脚本,您可以使用该upstart服务.请参阅相应的手册页和/etc/init/*.conf示例.创建此类过程后,您可以通过调用启动服务器
service myserver start
Run Code Online (Sandbox Code Playgroud)
如果您想要更多功能,例如特定限制或权限管理,您可以尝试xinetd.
你可以像这样开始你的过程:
nohup ./myexecutable &
Run Code Online (Sandbox Code Playgroud)
该&告诉shell在后台启动命令,保持它在任务列表.在某些shell上,如果父shell使用HANGUP信号退出,则作业将被终止.为防止这种情况,您可以使用nohup命令启动命令,该命令会丢弃HANGUP信号.
但是,如果被调用的进程重新连接HANGUP信号,则这不起作用.
要确定,您需要从shell的作业列表中删除该进程.对于两个众所周知的炮弹,可以通过以下方式实现:
./myexecutable &
disown <pid>
Run Code Online (Sandbox Code Playgroud)
./myexecutable &!
Run Code Online (Sandbox Code Playgroud)
通常,shell会打印进程的PID,然后可以使用该kill命令将其终止,以停止服务器.如果你的shell没有打印PID,你可以使用它
echo $!
Run Code Online (Sandbox Code Playgroud)
执行后直接.这将打印分叉进程的PID.
Ubuntu的?使用upstart.
/etc/init为您的作业创建一个名为的文件your-service-name.conf
start on net-device-up
exec /path/to/file --option
Run Code Online (Sandbox Code Playgroud)
您可以使用start your-service-name,以及:stop,restart,status
这将使用 来配置您的服务systemd,这不是一个全面的教程,而是一个如何设置的快速入门。
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
Run Code Online (Sandbox Code Playgroud)
启动服务
[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
Run Code Online (Sandbox Code Playgroud)
服务状态
sudo systemctl status deploy-webhook.service
Run Code Online (Sandbox Code Playgroud)
日志
journalctl -u deploy-webhook -e
Run Code Online (Sandbox Code Playgroud)