有一个等效于ps的go命令吗? [英] Is there a go command equivalent to ps?

查看:63
本文介绍了有一个等效于ps的go命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种方法来遍历PID列表,以扫描具有特定命令的进程.例如, ps ax 的列是

I want a way to iterate through a list of PIDs scanning for processes with a particular command. For example the columns of ps ax are

 PID TTY      STAT   TIME COMMAND

我想知道是否有一种方法可以确定给定PID号的PID的 COMMAND 列.

I was wondering if there was a way for me to determine the COMMAND column of a PID given its number.

推荐答案

Go语言与 ps 命令无关.

The Go language and the ps command are unrelated.

ps 命令是POSIX规范的一部分,并且在所有类似Unix的系统(包括Linux,Solaris,* BSD等)上可用.阅读 ps(1).它与您的操作系统(在Windows上可能没有)相关.阅读 操作系统:三件简单的书 进行学习有关操作系统的更多信息,以及一些Linux编程书籍,例如 ALP 了解有关Linux编程的更多信息.另请参见 intro(2)& syscalls(2)(并找到与它们等效的Go语言).

The ps command is part of POSIX specification, and available on all Unix-like systems (including Linux, Solaris, *BSD, ....). Read ps(1). It is related to your operating system (and you probably don't have it on Windows). Read Operating Systems: Three Easy Pieces to learn more about OSes, and some Linux programming book like ALP to learn more about Linux programming. See also intro(2) & syscalls(2) (and find the Go equivalent of them).

我想要一种方法来遍历PID列表,以扫描具有特定命令的进程.

I want a way to iterate through a list of PIDs scanning for processes with a particular command.

我想知道是否有一种方法可以确定给定PID号的PID的COMMAND列.

I was wondering if there was a way for me to determine the COMMAND column of a PID given its number.

这与Go无关.您可以使用/proc/伪文件系统,请参见 ps(1) top(1) pmap(1),等等...

This is unrelated to Go. You could use the /proc/ pseudo file system, see proc(5), which exists on all Linux systems, both with and without Go installed on them. /proc/ is internally used by ps(1), top(1), pmap(1), etc...

要遍历进程列表(在Linux上),您需要读取/proc/目录中的数字条目(例如,如果存在/proc/1234/有一个pid 1234的进程).要读取目录,请使用 opendir(3) readdir(3) ioutils 中.

To iterate on the list of processes (on Linux), you need to read the /proc/ directory for numerical entries (e.g. /proc/1234/ exists if there is a process of pid 1234). To read a directory, use opendir(3), readdir(3), closedir(3), stat(2) in C and they all have their Go equivalent e.g. in ioutils package.

尤其是对于进程1234,您可以阅读/proc/1234/cmdline (其中包含 NUL 个字节分隔的字符串).当然,您可以从某些Go程序中读取该文件.尝试 od -cx/proc/self/cmdline 命令(使用 od(1))了解该文件的格式...

In particular, for process 1234, you could read /proc/1234/cmdline (which contains NUL byte separated strings). Of course you could read that file from some Go program. Try the od -cx /proc/self/cmdline command (using od(1)) to understand the format of that file ...

/code//proc/中的伪文件是类似管道的"文件,具有明显的大小(如 ls(1) ...)设置为0,应按顺序读取,请参见这个.

Pseudofiles in /proc/ are "pipe-like", have an apparent size (as given by stat(2) or by ls(1)...) of 0, and should be read sequentially, see this.

这篇关于有一个等效于ps的go命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆