Cygwin: Get process state

0

When I run top in Cygwin I get:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

I would like to extract these columns for all processes:

PID PPID S COMMAND

ps gives PID, PPID, and COMMAND, but how do I get the 'S' column for all processes?

Edit:

I cannot use what I use on 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

Ole Tange

Posted 2015-03-23T09:43:23.320

Reputation: 3 034

Answers

1

By default top does not show the PPID (at least in CygWin). Besides that, you can use the batch mode of top by using the -b switch in combination with the -n 1 switch (run once). I used awk to skip the first lines and do a rough selection of the columns.

top -b -n 1 | awk 'NR>6 { print $1, $8, $12 }'

agtoever

Posted 2015-03-23T09:43:23.320

Reputation: 5 490

That seems to work, but is awfully slow (600 ms vs, ps' 20 ms). Can top be sped up? – Ole Tange – 2015-03-23T15:45:16.537

600 ms (or 0,6 sec) is about the same performance I get; top is slow, not awk. Also, on my Debian it goes really fast. I tried strace to find out what kernel calls it was lagging on, but that gave no much insight. I suppose the interaction between CygWin and the underlying Windows OS is causing the delay. No clue why ps is much faster though. – agtoever – 2015-03-23T15:58:11.040

With a bit more hacking (batch script) you could read out all /proc/<pid>/status files, showing all the columns you need. – agtoever – 2015-03-23T16:02:03.020

grep State /proc/*/status seems pretty fast. – Ole Tange – 2015-03-23T16:08:08.393

0

You can use ps -e -o pid,ppid,state,comm

Lambert

Posted 2015-03-23T09:43:23.320

Reputation: 269

See my edit why that does not work. – Ole Tange – 2015-03-23T13:26:00.163

What does ps --help gives you? – Lambert – 2015-03-23T13:35:03.510

See edit for --help – Ole Tange – 2015-03-23T13:37:36.277

Note that the OP is using Cygwin, which uses a striped down version of ps. – agtoever – 2015-03-23T15:04:52.027

0

This became the solution:

perl -ne '/Name/ and print"\n";/(Name|Pid|Ppid|State):\s+(\S+)/ and print "$2\t";' /proc/*/status

Thanks for agtoever for inspiration.

Ole Tange

Posted 2015-03-23T09:43:23.320

Reputation: 3 034