Home  >  Article  >  Backend Development  >  How golang uses the os/exec library to query the process

How golang uses the os/exec library to query the process

PHPz
PHPzOriginal
2023-04-23 10:22:491062browse

Go language is an open source programming language. It not only has a rich standard library and efficient concurrent programming support, but also has a powerful process processing library-os/exec. In this article, we will explain how to query processes using the os/exec library.

First, let us look at a simple example:

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))
}

In the above example, we used the os/exec library to create a Command object and executed the ps -ef command. The Output method of the Command object captures the standard output of the process into a byte array and returns it.

If you run this code, you will see a list of all processes running on your Linux system. But what if you just want to find the process named "myprocess"?

Before continuing, we need to know the format of the ps command output. The ps command outputs the content in the following format:

UID        PID   PPID  C STIME TTY          TIME CMD
root         1      0  0 10:38 ?        00:00:01 /sbin/init
root         2      0  0 10:38 ?        00:00:00 [kthreadd]

In this format, the second column is the ID of the process, and the tenth column is the name of the process. We can use these columns when querying processes.

Now, let’s modify the previous example to only display processes named “myprocess”:

package main

import (
    "fmt"
    "os/exec"
    "strings"
)

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

    lines := strings.Split(string(output), "\n")
    for _, line := range lines {
        if strings.Contains(line, "myprocess") {
            fmt.Println(line)
        }
    }
}

In this example, we first split the process list into rows using the strings.Split function Text. After that, we loop through each line and search for the line named "myprocess" in it. If found, we print this line.

Of course, this example is just an example. In real applications, you may need to filter processes based on different conditions. Fortunately, the os/exec library provides all the necessary tools.

Now, if you run this code, and you have a process named "myprocess" running, you should see output similar to the following:

myuser     1234     1  0 Apr09 ?        00:00:00 myprocess

Here, we process a Simple example showing how to query processes using the os/exec library. Of course, os/exec can do much more than that. No matter how complex your needs are, you can trust it to provide the necessary tools to handle the process.

The above is the detailed content of How golang uses the os/exec library to query the process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn