forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pid_file.go
43 lines (35 loc) · 898 Bytes
/
pid_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package servenv
import (
"flag"
"fmt"
"os"
log "github.com/golang/glog"
)
var pidFile = flag.String("pid_file", "", "If set, the process will write its pid to the named file, and delete it on graceful shutdown.")
func init() {
// Create pid file after flags are parsed.
onInit(func() {
if *pidFile == "" {
return
}
file, err := os.Create(*pidFile)
if err != nil {
log.Errorf("Unable to create pid file '%s': %v", *pidFile, err)
return
}
fmt.Fprintln(file, os.Getpid())
file.Close()
})
// Remove pid file on graceful shutdown.
OnClose(func() {
if *pidFile == "" {
return
}
if err := os.Remove(*pidFile); err != nil {
log.Errorf("Unable to remove pid file '%s': %v", *pidFile, err)
}
})
}