pid.go 419 B

12345678910111213141516171819202122232425
  1. package process
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. )
  7. func WritePIDFile(pidFile string) error {
  8. pid := os.Getpid()
  9. if pid == 0 {
  10. return fmt.Errorf("failed to get process ID")
  11. }
  12. pidStr := strconv.Itoa(pid)
  13. if err := os.WriteFile(pidFile, []byte(pidStr), 0644); err != nil {
  14. return fmt.Errorf("failed to write PID file: %w", err)
  15. }
  16. return nil
  17. }
  18. func RemovePIDFile(pidFile string) {
  19. _ = os.Remove(pidFile)
  20. }