1

When I run top I see a screen similar to this,

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    654 root      20   0   25.5g 147780  70084 S   3.7   0.9  72:08.69 Xorg
   1687 me        20   0 9165008   1.4g 268488 S   1.3   8.9 163:06.01 qutebrowser
    316 root     -51   0       0      0      0 S   0.7   0.0   1:09.86 irq/51-DELL095A:00
    683 me        20   0  810360  58212  41792 S   0.7   0.4  12:35.61 picom
    702 me        20   0  246876  18032  14616 S   0.7   0.1  26:04.86 i3bar
    827 me        20   0  665528  16316   9980 S   0.7   0.1  32:41.79 conky
2874827 me        20   0 6008688 209960 123060 S   0.7   1.3   0:10.84 QtWebEngineProc
    675 me        20   0  175796  24656  15216 S   0.3   0.2   0:14.27 i3
    771 me         9 -11 1022184  21952  12056 S   0.3   0.1  82:23.43 pulseaudio

but the program is interactive, so I see the screen changing as time passes (which is ok because the info does change over time). Furthermore, top | grep whatever doesn't seem to return ever.

What if I want a snapshot of that state for sending it to a text processing utility?

A comment suggested to go for top -b -n 1, but if I pipe that into some other program (or, for simplicity, if I > some-file), I see that it also has kind of heading,

top - 21:35:47 up 5 days,  7:24,  1 user,  load average: 0.16, 0.41, 0.37
Tasks: 243 total,   1 running, 241 sleeping,   1 stopped,   0 zombie
%Cpu(s):  2.3 us,  3.1 sy,  0.0 ni, 93.8 id,  0.0 wa,  0.0 hi,  0.8 si,  0.0 st
MiB Mem :  15787.2 total,   1840.9 free,   5693.1 used,   8253.3 buff/cache
MiB Swap:  16384.0 total,  16375.2 free,      8.8 used.   6256.7 avail Mem 

It would be nice to have a clean tabular report of all processes running, and nothing more.

Should I use an utility other than top?

Is maybe ps -e what I should reach for?


The scenario in which I plan to use this solution is for a PID picker for a debugger: the user would write the name of the process, and the PID picker would filter the list of running processes based on that; then when the user selects the desired process, the corresponding PID is used to attach the debugger.

5
  • 1
    top | grep is not supposed to return since top wont return. However top -n 1 | grep blahblah would. Commented Aug 7, 2022 at 7:49
  • Read the man page. On my Linux Mint, top -b -n 1 -u paul > foo runs in batch mode, runs one cycle, and writes a snapshot to the redirection (file or pipe). The Big Deal is that it omits all the ncurses terminal controls in -b mode. Commented Aug 7, 2022 at 7:57
  • 2
    Does this answer your question? terminate 'top' command after one iteration - Unix Commented Aug 7, 2022 at 10:58
  • 2
    There's probably better ways of getting the information you need than running top. Maybe ask that question separately with tags for whichever platform you're on and shell you're running? Commented Aug 7, 2022 at 15:27
  • Your question does not explain the reason for the snapshot, or how the components of the snapshot could be assessed in comparison to each other in order to make some relevant conclusion. As such, there is no basis on which one could attempt to make recommendations regarding the how to address the unspecified problem, or to select from the potential many alternate approaches to your problem. If you were looking to create a relationship tree for the processes, you might want to look at the pt command, which is distinct from the ps command with options to generate such a tree. Commented Sep 18, 2022 at 0:01

2 Answers 2

1

I have been pretty successful with the ps -o output format field names. The output specifiers are mentioned a couple of times early in the Linux ps(1) man page:

First a short ways into the EXAMPLES section:

To see every process with a user-defined format:
ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm ps -Ao pid,tt,user,fname,tmout,f,wchan

The man page's OUTPUT FORMAT CONTROL section has the detail for the option:

-o format
User-defined format. format is a single argument in the form of a blank-separated or comma-separated list, which offers a way to specify individual output columns. The recognized keywords are described in the STANDARD FORMAT SPECIFIERS section below. Headers may be renamed (ps -o pid,ruser=RealUser -o comm=Command) as desired. If all column headers are empty (ps -o pid= -o comm=) then the header line will not be output.

Examples:

Recently I used this as a lightweight way to get the resident set size (RAM usage) of processes for monitoring purposes:

ps -eo pid=,rss=,comm=

Example output:

3874689  9104 sshd
3875080  8788 sshd

Replacing comm= with args= produces:

3874689  9104 sshd: userone [priv]
3875080  8788 sshd: usertwo [priv]

I.e., args produces a command name with arguments, which is probably more expected from ps.

It's a good idea to read the man page in more detail about these options, including the STANDARD FORMAT SPECIFIERS section referenced in the -o description. For clarity, I didn't copy the whole description above. It's also a good idea to experiment with them and make sure you understand the output format, including separator characters.

Other uses:

I have used ps -o with the %cpu and %mem specifiers as well as etime, etimes, bsdstart, and lstart. It can be very useful when you want the process listing features of ps but want simpler output to parse in your script/code.

0

Eventually I used the output of ps -e.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.