pkg.re/essentialkaos/ek@v12.36.0+incompatible/pid/example_test.go (about) 1 package pid 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2021 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 ) 13 14 // ////////////////////////////////////////////////////////////////////////////////// // 15 16 func ExampleCreate() { 17 // You can set default derectory for pid files 18 Dir = "/home/user/my-pids" 19 20 err := Create("servicename") 21 22 if err != nil { 23 fmt.Printf("Error: %v\n", err) 24 } 25 26 fmt.Println("PID file created!") 27 } 28 29 func ExampleRemove() { 30 err := Remove("servicename") 31 32 if err != nil { 33 fmt.Printf("Error: %v\n", err) 34 } 35 36 fmt.Println("PID file removed!") 37 } 38 39 func ExampleGet() { 40 pid := Get("servicename") 41 42 if pid == -1 { 43 fmt.Println("Can't read PID from PID file") 44 } 45 46 fmt.Printf("PID is %d\n", pid) 47 } 48 49 func ExampleRead() { 50 pid := Read("/var/run/httpd.pid") 51 52 if pid == -1 { 53 fmt.Println("Can't read PID from PID file") 54 } 55 56 fmt.Printf("PID is %d\n", pid) 57 } 58 59 func ExampleIsProcessWorks() { 60 pid := 1234 61 62 if IsProcessWorks(pid) { 63 fmt.Printf("Process with PID %d is works\n", pid) 64 } else { 65 fmt.Printf("Process with PID %d isn't working\n", pid) 66 } 67 } 68 69 func ExampleIsWorks() { 70 if IsWorks("servicename") { 71 fmt.Println("Process is works") 72 } else { 73 fmt.Println("Process isn't working") 74 } 75 }