golang使用os/exec库查询进程

本文主要介绍如何使用os/exec库查询进程。

package mainimport ("fmt""os/exec")func main() {cmd := exec.Command("ps", "-ef")output, err := cmd.Output()if err != nil {fmt.Println(err)return}fmt.Println(string(output))}

输出:

 go run process.goUID   PID  PPID   C STIME   TTY           TIME CMD0     1     0   0  1 170  ??       114:31.61 /sbin/launchd0   287     1   0  1 476  ??        21:22.98 /usr/libexec/logd0   288     1   0  1 476  ??         1:09.95 /usr/libexec/UserEventAgent (System)0   290     1   0  1 476  ??         0:23.88 /System/Library/PrivateFrameworks/Uninstall.framework/Resources/uninstalld0   291     1   0  1 476  ??        24:42.10 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/Support/fseventsd0   292     1   0  1 476  ??        28:48.91 /Library/Application Support/Symantec/Silo/MES/Daemon/SymDaemon0   296     1   0  1 476  ??         1:01.12 /Library/PrivilegedHelperTools/AliCloudPrinterUpdater0   298     1   0  1 476  ??        57:11.04 /usr/sbin/systemstats --daemon0   300     1   0  1 476  ??        13:41.30 /usr/libexec/configd0   301     1   0  1 476  ??        14:46.12 /Library/Application Support/AntGroup/starpoint/Mobius0   303     1   0  1 476  ??         6:36.35 /System/Library/CoreServices/powerd.bundle/powerd501   304     1   0  1 476  ??         0:08.48 /Library/McAfee/agent/bin/macmnsvc self_start0   305     1   0  1 476  ??         1:48.76 /Library/McAfee/agent/bin/masvc self_start0   306     1   0  1 476  ??         0:00.01 /usr/libexec/IOMFB_bics_daemon0   309     1   0  1 476  ??         0:00.59 /usr/libexec/remoted0   317     1   0  1 476  ??         0:30.29 /usr/libexec/watchdogd0   321     1   0  1 476  ??        25:27.64 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support/mds0   323     1   0  1 476  ??         0:01.38 /usr/libexec/kernelmanagerd0   324     1   0  1 476  ??         0:43.61 /usr/libexec/diskarbitrationd0   330     1   0  1 476  ??         0:31.26 /usr/sbin/syslogd0   333     1   0  1 476  ??         1:51.28 /usr/libexec/thermalmonitord0   334     1   0  1 476  ??        27:10.87 /usr/libexec/opendirectoryd0   335     1   0  1 476  ??         1:15.07 /System/Library/PrivateFrameworks/ApplePushService.framework/apsd0   337     1   0  1 476  ??         0:01.15 /Library/PrivilegedHelperTools/com.docker.vmnetd0   338     1   0  1 476  ??         9:32.88 /System/Library/CoreServices/launchservicesd213   341     1   0  1 476  ??         0:01.83 /System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/Resources/usbmuxd -launchd0   342     1   0  1 476  ??         1:12.54 /usr/sbin/securityd -i0   343     1   0  1 476  ??         0:00.03 auditd -l

上述代码逻辑会获取到当前系统中所有运行的进程,但如果只需要获取制定进程信息呢,就需要针对进程进行过滤。

第一种方式:

package mainimport ("fmt""bytes""os/exec")
func RunCmd(cmdstring string) (string, error) {var out bytes.Buffervar stderr bytes.Buffercmd := exec.Command("/bin/sh", "-c", cmdstring)cmd.Stdout = &outcmd.Stderr = &stderrerr := cmd.Run()if err != nil {fmt.Printf("err:%v\n",err)return fmt.Sprintf("%s",stderr.String()),err}return fmt.Sprintf("%v",out.String()),nil
}func main() {cmd := fmt.Sprintf("ps -ef | grep '%v'| grep -v grep","/bin/zsh --login -i")result,err := RunCmd(cmd)if err != nil {fmt.Printf("run cmd:%v failed:%v\n",cmd,err)return}fmt.Println(result)}

输出:

$ go run process.go502 45987 45915   0  8 623  ttys004    0:00.09 /bin/zsh --login -i502 76923 45915   0 13 623  ttys009    0:00.07 /bin/zsh --login -i502 39231 45915   0 三02下午 ttys012    0:00.08 /bin/zsh --login -i

第二种方式:

package mainimport ("fmt""strings""bytes""os/exec")
func RunCmd(cmdstring string) (string, error) {var out bytes.Buffervar stderr bytes.Buffercmd := exec.Command("/bin/sh", "-c", cmdstring)cmd.Stdout = &outcmd.Stderr = &stderrerr := cmd.Run()if err != nil {fmt.Printf("err:%v\n",err)return fmt.Sprintf("%s",stderr.String()),err}return fmt.Sprintf("%v",out.String()),nil
}func main() {//cmd := fmt.Sprintf("ps -ef | grep '%v'| grep -v grep","/bin/zsh --login -i")cmd := fmt.Sprintf("ps -ef")result,err := RunCmd(cmd)if err != nil {fmt.Printf("run cmd:%v failed:%v\n",cmd,err)return}lines := strings.Split(result, "\n")for _, line := range lines {if strings.Contains(line, "/bin/zsh --login -i") {fmt.Println(line)}}
}

输出:

 $ go run process.go502 45987 45915   0  8 623  ttys004    0:00.09 /bin/zsh --login -i502 76923 45915   0 13 623  ttys009    0:00.07 /bin/zsh --login -i502 39231 45915   0 三02下午 ttys012    0:00.08 /bin/zsh --login -i

本文链接:https://my.lmcjl.com/post/14112.html

展开阅读全文

4 评论

留下您的评论.