开发者

Linux ps Output Format Spec

I am using Java API to connect my Remote Machine so I can see the running processes.

One of the requirements is to be able to kill any of the processes.

Now I am executing command

ps aux | grep java which return list of the running processes.

eg.

root       330  0.2  开发者_开发问答0.0     0    0 pts/0    Z    08:42   0:11 [java] 

Does anyone know what is the spec is used for above output ? I'll need to convert above line into an object where 330 will the process id.


Maybe you can use something like this:

ps -ef | grep java | awk -F" " '{print $2}'

Or specify the format yourself (e.g. pid and command only):

ps -eo pid,comm | grep java | awk -F" " '{print $1}'

If the command with arguments is need for grep:

ps -eo pid,command | grep java | awk -F" " '{print $1}'


If you are only intresseted in the pid of the processes with that name check pgrep.


Example:

$ pgrep sshd
791
22956
23060


On my Ubuntu system, ps says it's in line with these standards:

STANDARDS This ps conforms to:

 1   Version 2 of the Single Unix Specification
 2   The Open Group Technical Standard Base Specifications, Issue 6
 3   IEEE Std 1003.1, 2004 Edition
 4   X/Open System Interfaces Extension [UP XSI]
 5   ISO/IEC 9945:2003</blockquote>

But you might consider offloading the problem to pgrep, which is already maintained and already understands about process names and such. It gives you a much, much simpler output: By default, just the matching process IDs, one per line, like this:

$ pgrep apache
3990
22244
22388
22391
22476

Doesn't get easier to parse than that. If you need to see more, you might consider the -l flag:

$ pgrep -l apache
3990 apache2
22244 apache2
22388 apache2
22391 apache2
22476 apache2

Also consider looking at the /proc filesystem, which is where ps looks for its data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜