Read reads the "PID file" at path, and returns the PID if it contains a valid PID of a running process, or 0 otherwise. It returns an error when failing to read the file, or if the file doesn't exist, but malformed content is ignored. Consumers should therefore check if the returned PID is a non-zer
(path string)
| 35 | // is ignored. Consumers should therefore check if the returned PID is a non-zero |
| 36 | // value before use. |
| 37 | func Read(path string) (pid int, _ error) { |
| 38 | pidByte, err := os.ReadFile(path) |
| 39 | if err != nil { |
| 40 | return 0, err |
| 41 | } |
| 42 | pid, err = strconv.Atoi(string(bytes.TrimSpace(pidByte))) |
| 43 | if err != nil { |
| 44 | return 0, nil |
| 45 | } |
| 46 | if pid != 0 && alive(pid) { |
| 47 | return pid, nil |
| 48 | } |
| 49 | return 0, nil |
| 50 | } |
| 51 | |
| 52 | // Write writes a "PID file" at the specified path. It returns an error if the |
| 53 | // file exists and contains a valid PID of a running process, or when failing |