moooofly / hunter-agent

This is an agent for hunter system as a proxy.
MIT License
2 stars 0 forks source link

[BUG] Error starting daemon: pid file found, ensure hunter-agent is not running or delete /var/run/hunter-agent.pid #3

Open moooofly opened 6 years ago

moooofly commented 6 years ago

phenomenon

[#970#root@ubuntu-1604 /go/src/github.com/moooofly/hunter-agent]$docker run -it -p 12345:12345 -v /var/run:/var/run --rm hunter-agent:tmp -H tcp://0.0.0.0:12345 -H unix:///var/run/hunter-agent.sock --metrics-addr 0.0.0.0:12346 --broker xx.xx.xx.xx:9092 --topic jaeger-spans-test-001
Error starting daemon: pid file found, ensure hunter-agent is not running or delete /var/run/hunter-agent.pid
[#971#root@ubuntu-1604 /go/src/github.com/moooofly/hunter-agent]$

solution

[#971#root@ubuntu-1604 /go/src/github.com/moooofly/hunter-agent]$rm /var/run/hunter-agent.pid

[#972#root@ubuntu-1604 /go/src/github.com/moooofly/hunter-agent]$docker run -it -p 12345:12345 -v /var/run:/var/run --rm hunter-agent:tmp -H tcp://0.0.0.0:12345 -H unix:///var/run/hunter-agent.sock --metrics-addr 0.0.0.0:12346 --broker xx.xx.xx.xx:9092 --topic jaeger-spans-test-001
DEBU[2018-08-23T08:45:05.727044578Z] --> Serve [::]:12345
INFO[2018-08-23T08:45:05.727090605Z] Listener created on tcp (0.0.0.0:12345)
DEBU[2018-08-23T08:45:05.727150482Z] --> Serve /var/run/hunter-agent.sock
INFO[2018-08-23T08:45:05.727163209Z] Listener created on unix (/var/run/hunter-agent.sock)
DEBU[2018-08-23T08:45:05.731410016Z] --> config.Root: /var/lib/hunter
DEBU[2018-08-23T08:45:05.734990555Z] Golang's threads limit set to 6930
INFO[2018-08-23T08:45:05.735049956Z] Hunter agent                                  commit=library-import version=library-import
INFO[2018-08-23T08:45:05.735070403Z] Daemon has completed initialization
...

todo

Need a better way to clear /var/run/hunter-agent.pid file.

moooofly commented 6 years ago
    if cli.Pidfile != "" {
        pf, err := pidfile.New(cli.Pidfile)
        if err != nil {
            return fmt.Errorf("Error starting daemon: %v", err)      -- 1
        }
        defer func() {
            if err := pf.Remove(); err != nil {        -- 4
                logrus.Error(err)
            }
        }()
    }
func checkPIDFileAlreadyExists(path string) error {
    if pidByte, err := ioutil.ReadFile(path); err == nil {
        pidString := strings.TrimSpace(string(pidByte))
        if pid, err := strconv.Atoi(pidString); err == nil {
            if processExists(pid) {
                return fmt.Errorf("pid file found, ensure hunter-agent is not running or delete %s", path)      -- 3
            }
        }
    }
    return nil
}

// New creates a PIDfile using the specified path.
func New(path string) (*PIDFile, error) {
    if err := checkPIDFileAlreadyExists(path); err != nil {      -- 2
        return nil, err
    }
    // Note MkdirAll returns nil if a directory already exists
    if err := system.MkdirAll(filepath.Dir(path), os.FileMode(0755), ""); err != nil {
        return nil, err
    }
    if err := ioutil.WriteFile(path, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
        return nil, err
    }

    return &PIDFile{path: path}, nil
}

// Remove removes the PIDFile.
func (file PIDFile) Remove() error {
    return os.Remove(file.path)       -- 5
}

As the codes show above, if the daemon exit normally, pf.Remove() will be called by defer, unless it is killed brutally by kill -9.

But even kill -9, it seems that the daemon can still startup normally.