当我top
在 Cygwin 中运行时,我得到:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
我想提取所有流程的这些列:
PID PPID S COMMAND
ps
给出 PID、PPID 和 COMMAND,但是如何获取所有进程的“S”列?
编辑:
我无法使用在 GNU/Linux 上使用的内容:
$ ps -e -o pid,ppid,state,comm
ps: unknown option -- o
Try `ps --help' for more information.
$ ps --version
ps (cygwin) 1.7.33
Show process statistics
Copyright (C) 1996 - 2014 Red Hat, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ps --help
Usage: ps [-aefls] [-u UID] [-p PID]
Report process status
-a, --all show processes of all users
-e, --everyone show processes of all users
-f, --full show process uids, ppids
-h, --help output usage information and exit
-l, --long show process uids, ppids, pgids, winpids
-p, --process show information for specified PID
-s, --summary show process summary
-u, --user list processes owned by UID
-V, --version output version information and exit
-W, --windows show windows as well as cygwin processes
With no options, ps outputs the long format by default
procps
:
version: 3.2.8-3
答案1
默认情况下top
不显示 PPID(至少在 CygWin 中)。除此之外,您可以通过将开关与开关(运行一次)-b
结合使用来使用 top 的批处理模式。我过去常常跳过前几行并粗略地选择列。-n 1
awk
top -b -n 1 | awk 'NR>6 { print $1, $8, $12 }'
答案2
您可以使用ps -e -o pid,ppid,state,comm
答案3
解决方案如下:
perl -ne '/Name/ and print"\n";/(Name|Pid|Ppid|State):\s+(\S+)/ and print "$2\t";' /proc/*/status
感谢 agtoever 给予的启发。