87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
// pid
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
pid = PID{}
|
|
)
|
|
|
|
type PID struct {
|
|
PID string
|
|
PIDFile string
|
|
}
|
|
|
|
// verifica se esiste il PIDFile;
|
|
// se esiste legge il PID e controlla se e' running il processo associato
|
|
func (p *PID) check() bool {
|
|
bpid, err := ioutil.ReadFile(p.PIDFile)
|
|
p.PID = strings.TrimRight(string(bpid), "\n")
|
|
if err == nil && p.readCmd() {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// controlla se esiste il processo associato al PID,
|
|
// se il cmd e' lo stesso e se e' in esecuzione.
|
|
func (p *PID) readCmd() bool {
|
|
bcmd, err := ioutil.ReadFile(path.Join("/proc", p.PID, "cmdline"))
|
|
// non esiste la dir relativa al PID su /proc
|
|
if err != nil {
|
|
fmt.Println("cmdline error: ", err)
|
|
return false
|
|
}
|
|
cmd := bytes.Trim(bcmd, "\x00")
|
|
if strings.Contains(string(cmd), opts.Exe) {
|
|
return true
|
|
} else {
|
|
fmt.Printf("PID %s used by %s\n", pid, cmd)
|
|
return true
|
|
}
|
|
return true
|
|
}
|
|
|
|
// scrive il PID nel PIDFile
|
|
func (p *PID) Write(l bool) {
|
|
|
|
if p.check() {
|
|
if l {
|
|
log.Println("Running: ", p.PID)
|
|
} else {
|
|
fmt.Println("Running: ", p.PID)
|
|
}
|
|
|
|
os.Exit(-6)
|
|
}
|
|
|
|
fpid, err := os.OpenFile(p.PIDFile, os.O_WRONLY|os.O_CREATE, 0666)
|
|
if err != nil {
|
|
if l {
|
|
log.Println("PID file error: ", err.Error())
|
|
} else {
|
|
fmt.Println("PID file error: ", err.Error())
|
|
}
|
|
|
|
os.Exit(-5)
|
|
}
|
|
fpid.WriteString(strconv.Itoa(os.Getpid()))
|
|
fpid.Close()
|
|
}
|
|
|
|
// Cancella il PIDFile
|
|
func (p *PID) Remove() {
|
|
err := os.Remove(p.PIDFile)
|
|
if err != nil {
|
|
fmt.Println("RM file error: ", err.Error())
|
|
}
|
|
}
|