Skip to content

Commit

Permalink
remove pidfile when error
Browse files Browse the repository at this point in the history
  • Loading branch information
kazeburo committed Feb 22, 2019
1 parent dc92e56 commit b433072
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pidfile/pidfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ func WritePid(pidfile string) error {
dir, filename := filepath.Split(pidfile)
tmpfile, err := ioutil.TempFile(dir, filename+".*")
if err != nil {
return errors.Wrap(err, "Cloud not create tempfile")
return errors.Wrap(err, "Could not create tempfile")
}
_, err = tmpfile.WriteString(fmt.Sprintf("%d", os.Getpid()))
if err != nil {
tmpfile.Close()
return errors.Wrap(err, "Cloud not write pid to tempfile")
os.Remove(tmpfile.Name())
return errors.Wrap(err, "Could not write pid to tempfile")
}
tmpfile.Close()
err = os.Rename(tmpfile.Name(), pidfile)
if err != nil {
return errors.Wrap(err, "Cloud not rename pidfile")
os.Remove(tmpfile.Name())
return errors.Wrap(err, "Could not rename pidfile")
}
return nil
}

0 comments on commit b433072

Please sign in to comment.