proc.go 786 B

12345678910111213141516171819202122232425262728
  1. package system
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. )
  8. // look in /proc to find the process start time so that we can verify
  9. // that this pid has started after ourself
  10. func GetProcessStartTime(pid int) (string, error) {
  11. data, err := ioutil.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "stat"))
  12. if err != nil {
  13. return "", err
  14. }
  15. parts := strings.Split(string(data), " ")
  16. // the starttime is located at pos 22
  17. // from the man page
  18. //
  19. // starttime %llu (was %lu before Linux 2.6)
  20. // (22) The time the process started after system boot. In kernels before Linux 2.6, this
  21. // value was expressed in jiffies. Since Linux 2.6, the value is expressed in clock ticks
  22. // (divide by sysconf(_SC_CLK_TCK)).
  23. return parts[22-1], nil // starts at 1
  24. }