1

For Ubuntu Bionic, is there a tool that will, like top, show various data on processes and refresh automatically, but can be configured from the command line to specify what data (columns) to show?

Kusalananda
  • 333,661

2 Answers2

1

procps top, the one found on Ubuntu is probably the most configurable of top implementations.

To change the list of fields, press f and you can add remove fields, change their orders, etc.

There is a lot more you can change there, including colour, layout, including multi-pane views, the header at the top...

You can save those configuration by pressing W.

That will go to ~/.toprc (or ~/.config/procps/toprc in newer versions).

You can save them as a different profile by calling top with a different name. For instance, if you make a mytop symlink to the top executable, and invoke it, or run top as (exec -a mytop top), then W will save the configuration to ~/.mytoprc instead, so you can define a variety of differently flavoured versions of top that way.

0

It's a dilemma, isn't it?

  • top has automatic refresh, but no way to specify the columns to display.
  • ps and its ilk can be told what columns to output, but do not continually update.

I am part of the way towards addressing this for myself.

I already have a tool that can take the data of a table input over a pipe, and display it full screen on a terminal in a scrollable columnar format. It is the console-flat-table-viewer command from the nosh toolset. Feed it at intervals on its standard input a succession of tables, delimited by the relevant file separator character, and it will provide a continually updated display.

Note that this is not like watch. watch takes any old input. console-flat-table-viewer expects its input to be a table, in one of the several well-known flat file encodings that one find on Unices and Linux operating systems, and its user interface presents it as a table, aligned into rows and columns and with headings and a cursor.

The other part of the mechanism is a tool that emits the process table in such a flat file form in the first place, repeatedly at intervals, interspersing its output with a file separator. We almost, but do not quite, have this.

One can feed it the output of the top command in its "batch" mode:

while top -b all | sed -e '1,8s/^/#/' -e $'1i\\n\\f'
do
    sleep 1
done | 
console-flat-table-viewer --header-count 1

The sed turns into comments the part of the top output that is not actually the process table, which would otherwise confuse the table layout, and adds in the file separator. In the table encoding that matches what top outputs, what the console-flat-table-viewer manual calls the space format, the file separator is the ␌ character, which has to be escaped for sed to not strip it. (ASCII has a real ␜ character, which console-flat-table-viewer understands for ascii tables. This is not the encoding that top outputs, though.)

I add the ␌ character with sed to reduce display flicker, so that console-flat-table-viewer does not receive it until immediately before the rest of the table data. An alternative, which flickers slightly more because there is an interval between the ␌ and the table data during which console-flat-table-viewer displays a blank table, is to simply invoke printf first.

Similarly, the commands that generate the output go in the condition of the while loop, so that the broken pipe when I quit console-flat-table-viewer causes the loop to gracefully terminate.

But this does not provide control over columns.

One can alternatively feed it the output of the FreeBSD procstat command:

while printf '\f' ; procstat -a
do
    sleep 1
done | 
console-flat-table-viewer --header-count 1

This has a limited degree of control over columns with its various options, but not fine grained control. It is also somewhat problematic because several of its options cause it to break the tabular format by emitting unescaped whitespace in the middle of fields.

One could use the BSD ps command:

while ps -a -x -o "pid,ppid,user,logname,time,state,wchan,start,comm" | sed -e $'1i\\n\\f'
do
    sleep 1
done |
console-flat-table-viewer --header-count 1

The GNU-licensed ps command on Ubuntu Linux has a different command-line syntax, which is several questions and answers in its own right, but one could employ it likewise.

Whilst this does have fine-grained control over the columns, it again does not properly escape whitespace within fields.

The BSDs have had a way of dealing with this since 4.4BSD in the 1990s. It is the vis() encoding system, which various BSD flat tables (such as /etc/fstab) already employ. console-flat-table-viewer already knows how to decode it, too. It would be very welcome for ps or procstat, preferably both, to be able to vis-encode their outputs.

The obvious next step for GUI users is to have a (currently hypothetical) utility that substitutes for console-flat-table-viewer, taking the same input and leaving the input side of the pipeline the same, and displays continually updated tables with a GUI instead of a TUI.

Then we could have a flexible mechanism made of composable tools joined together with pipes. ☺

Further reading

JdeBP
  • 68,745