Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何配合systemd使用 #6

Closed
Vortexxxx opened this issue Apr 12, 2018 · 6 comments
Closed

请问如何配合systemd使用 #6

Vortexxxx opened this issue Apr 12, 2018 · 6 comments

Comments

@Vortexxxx
Copy link

这可能不是一个关于gracehttp本身的问题

我尝试将readme中的demo配置systemd一起使用,写了一个简单的service文件

[Unit]
Description=test server

[Service]
User=www-data
Group=www-data
ExecStart=/usr/local/test-server
ExecReload=/bin/kill -SIGUSR2 $MAINPID
PIDFile=/tmp/test-server.pid

[Install]
WantedBy=multi-user.target

在代码中我添加了/tmp/test-server.pid文件的输出,代码如下:

package main

import (
	"fmt"
	"net/http"
	"os"
	"syscall"
	"time"

	"github.com/tabalt/gracehttp"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(10 * time.Second)
		fmt.Fprintf(w, "hello world")
	})
	outputFile, err := os.OpenFile("/tmp/test-server.pid", os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Printf("An error occurred with file opening or creation\n")
		return
	}
	defer outputFile.Close()
	fmt.Fprintln(outputFile, syscall.Getegid())

	err = gracehttp.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println(err)
	}
}

第一次启动时一切都OK,但是当我执行systemctl reload test-server时报错:

Apr 12 15:51:56 ubuntu systemd[1]: Started didi web server.
Apr 12 15:53:51 ubuntu systemd[1]: Reloading didi web server.
Apr 12 15:53:51 ubuntu test-server[12013]: 2018/04/12 15:53:51 [pid 12013] received SIGUSR2, graceful restarting HTTP server.
Apr 12 15:53:51 ubuntu systemd[1]: test-server.service: Supervising process 33 which is not our child. We'll most likely not notice when it exits.
Apr 12 15:53:51 ubuntu systemd[1]: Reloaded web server.

这个时候程序还在正常运行,但是如果再次执行systemctl reload test-server时就会直接报错:

Job for test-server.service failed because the control process exited with error code. See "systemctl status test-server.service" and "journalctl -xe" for details.

另外在我的测试中,直接用systemd启动后不使用systemctl reload test-server而直接往pid发送-SIGUSR2是可以正常工作的

@hellojukay
Copy link

/tmp/test-server.pid的值是不是0

@Vortexxxx
Copy link
Author

@hellojukay 感谢回复

不是,执行systemctl reload test-server 之后Main PID变成了 33 (watchdog/5)/tmp/test-server.pid文件内也是这个pid

@tabalt
Copy link
Owner

tabalt commented Apr 13, 2018

看现象是systemd的报错,网上也搜到很多相同的问题。
不过我对systemd不熟,你的报错信息中,提到可以通过 "systemctl status test-server.service" 和"journalctl -xe" 看详细信息,是否有看详细信息是什么呢?

@Vortexxxx
Copy link
Author

@tabalt 感谢回复

通过systemctl status test-server.service看到的报错就是之前我贴过的这段:

Apr 12 15:51:56 ubuntu systemd[1]: Started didi web server.
Apr 12 15:53:51 ubuntu systemd[1]: Reloading didi web server.
Apr 12 15:53:51 ubuntu test-server[12013]: 2018/04/12 15:53:51 [pid 12013] received SIGUSR2, graceful restarting HTTP server.
Apr 12 15:53:51 ubuntu systemd[1]: test-server.service: Supervising process 33 which is not our child. We'll most likely not notice when it exits.
Apr 12 15:53:51 ubuntu systemd[1]: Reloaded web server.

我猜应该是进程退出的时候systemd没办法正确的找到fork出来的新进程,因为我在另外一个类似的组件endless的issue中看到有人说需要在systemd的配置文件中加上 PIDFile 这项�配置以便systemd找到新fork的进程号

但是如你所见,我已经配置了但是还不生效

既然如此,请教下你们生产环境中是用什么做守护进程的呢? Supervisor

@tabalt
Copy link
Owner

tabalt commented Apr 14, 2018

@Vortexxxx 我们目前是自研的一个简单的程序 https://github.com/tabalt/pmon
Supervisor 是以子进程的方式启动的业务进程,重启后就不是它的子进程了

@Vortexxxx
Copy link
Author

@tabalt 感谢,我去试试这个,这个issue我就关闭了, 再次感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants