ps.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. package robotgo
  11. import ps "github.com/vcaesar/gops"
  12. // Nps process struct
  13. type Nps struct {
  14. Pid int
  15. Name string
  16. }
  17. // Pids get the all process id
  18. func Pids() ([]int, error) {
  19. return ps.Pids()
  20. }
  21. // PidExists determine whether the process exists
  22. func PidExists(pid int) (bool, error) {
  23. return ps.PidExists(pid)
  24. }
  25. // Process get the all process struct
  26. func Process() ([]Nps, error) {
  27. var npsArr []Nps
  28. nps, err := ps.Process()
  29. for i := 0; i < len(nps); i++ {
  30. np := Nps{
  31. nps[i].Pid,
  32. nps[i].Name,
  33. }
  34. npsArr = append(npsArr, np)
  35. }
  36. return npsArr, err
  37. }
  38. // FindName find the process name by the process id
  39. func FindName(pid int) (string, error) {
  40. return ps.FindName(pid)
  41. }
  42. // FindNames find the all process name
  43. func FindNames() ([]string, error) {
  44. return ps.FindNames()
  45. }
  46. // FindIds finds the all processes named with a subset
  47. // of "name" (case insensitive),
  48. // return matched IDs.
  49. func FindIds(name string) ([]int, error) {
  50. return ps.FindIds(name)
  51. }
  52. // FindPath find the process path by the process pid
  53. func FindPath(pid int) (string, error) {
  54. return ps.FindPath(pid)
  55. }
  56. // Run run a cmd shell
  57. func Run(path string) ([]byte, error) {
  58. return ps.Run(path)
  59. }
  60. // Kill kill the process by PID
  61. func Kill(pid int) error {
  62. return ps.Kill(pid)
  63. }