本文主要介绍如何使用os/exec库查询进程。
package main
import (
"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.go
UID PID PPID C STIME TTY TIME CMD
0 1 0 0 1 170 ?? 114:31.61 /sbin/launchd
0 287 1 0 1 476 ?? 21:22.98 /usr/libexec/logd
0 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/uninstalld
0 291 1 0 1 476 ?? 24:42.10 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/Support/fseventsd
0 292 1 0 1 476 ?? 28:48.91 /Library/Application Support/Symantec/Silo/MES/Daemon/SymDaemon
0 296 1 0 1 476 ?? 1:01.12 /Library/PrivilegedHelperTools/AliCloudPrinterUpdater
0 298 1 0 1 476 ?? 57:11.04 /usr/sbin/systemstats --daemon
0 300 1 0 1 476 ?? 13:41.30 /usr/libexec/configd
0 301 1 0 1 476 ?? 14:46.12 /Library/Application Support/AntGroup/starpoint/Mobius
0 303 1 0 1 476 ?? 6:36.35 /System/Library/CoreServices/powerd.bundle/powerd
501 304 1 0 1 476 ?? 0:08.48 /Library/McAfee/agent/bin/macmnsvc self_start
0 305 1 0 1 476 ?? 1:48.76 /Library/McAfee/agent/bin/masvc self_start
0 306 1 0 1 476 ?? 0:00.01 /usr/libexec/IOMFB_bics_daemon
0 309 1 0 1 476 ?? 0:00.59 /usr/libexec/remoted
0 317 1 0 1 476 ?? 0:30.29 /usr/libexec/watchdogd
0 321 1 0 1 476 ?? 25:27.64 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support/mds
0 323 1 0 1 476 ?? 0:01.38 /usr/libexec/kernelmanagerd
0 324 1 0 1 476 ?? 0:43.61 /usr/libexec/diskarbitrationd
0 330 1 0 1 476 ?? 0:31.26 /usr/sbin/syslogd
0 333 1 0 1 476 ?? 1:51.28 /usr/libexec/thermalmonitord
0 334 1 0 1 476 ?? 27:10.87 /usr/libexec/opendirectoryd
0 335 1 0 1 476 ?? 1:15.07 /System/Library/PrivateFrameworks/ApplePushService.framework/apsd
0 337 1 0 1 476 ?? 0:01.15 /Library/PrivilegedHelperTools/com.docker.vmnetd
0 338 1 0 1 476 ?? 9:32.88 /System/Library/CoreServices/launchservicesd
213 341 1 0 1 476 ?? 0:01.83 /System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/Resources/usbmuxd -launchd
0 342 1 0 1 476 ?? 1:12.54 /usr/sbin/securityd -i
0 343 1 0 1 476 ?? 0:00.03 auditd -l
上述代码逻辑会获取到当前系统中所有运行的进程,但如果只需要获取制定进程信息呢,就需要针对进程进行过滤。
第一种方式:
package main
import (
"fmt"
"bytes"
"os/exec"
)
func RunCmd(cmdstring string) (string, error) {
var out bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("/bin/sh", "-c", cmdstring)
cmd.Stdout = &out
cmd.Stderr = &stderr
err := 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.go
502 45987 45915 0 8 623 ttys004 0:00.09 /bin/zsh --login -i
502 76923 45915 0 13 623 ttys009 0:00.07 /bin/zsh --login -i
502 39231 45915 0 三02下午 ttys012 0:00.08 /bin/zsh --login -i
第二种方式:文章来源:https://www.toymoban.com/news/detail-506687.html
package main
import (
"fmt"
"strings"
"bytes"
"os/exec"
)
func RunCmd(cmdstring string) (string, error) {
var out bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("/bin/sh", "-c", cmdstring)
cmd.Stdout = &out
cmd.Stderr = &stderr
err := 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)
}
}
}
输出:文章来源地址https://www.toymoban.com/news/detail-506687.html
$ go run process.go
502 45987 45915 0 8 623 ttys004 0:00.09 /bin/zsh --login -i
502 76923 45915 0 13 623 ttys009 0:00.07 /bin/zsh --login -i
502 39231 45915 0 三02下午 ttys012 0:00.08 /bin/zsh --login -i
到了这里,关于golang使用os/exec库查询进程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!