'top add filter commands via command line options
I'm trying to add filter commands to top for non-interactive usage. While using top, it is possible to specify filters interactively, e.g.
$ top
Type: o
Enter: COMMAND=MyProcess
will show only those processes with "MyProcss" in the command.
Further, top's display settings can be saved to file with "W" from the command line to $HOME/.toprc. Then when top is started again, it will use those display settings instead of the default.
But it seems that filters which are added via "COMMAND=..." are not saved in the .toprc file. So is it possible to add these filters via command line so that top can output the data non-interactively? I can't use top's "-p" option to display data from specific PIDs since I have more than 20 PIDs that need to be tracked, and they could be added after top is invoked. Also, I don't want to output data for all 900+ processes that are running on my machine.
Finally, I don't really want to just grep the process names since I want the header of top as well.
So ideally, the pseudo command would be like, but this doesn't work:
top -b -o 'COMMAND=MyProcess' -o '!COMMAND=NotThisProcess'
Solution 1:[1]
According to my top man page, -o is for sort order only. I see no command line options to specify an "other" filter. And the -b option precludes reading user input to type in a filter.
Assuming your top is like mine, there are 7 lines of headers. So if you want to only see "MyProcess" processes, you can do this:
top -b -n 1 | sed '8,$ {/MyProcess/! d}'
Meaning, from lines 8 to the end, delete any line that does not contain MyProcess.
Solution 2:[2]
What top version are you using (top -v)? This matters since some versions state that "active 'other filters'" are saved in the PERSONAL Configuration File, while others do not. For example, compare the top man page on man7.org vs the one on manpages.ubuntu.com
For me, top -v returns procps-ng 3.3.12
Looking at the procps-ng repository I can see that there was an issue to add support for this. That issue was resolved and support for saving Other Filters to the config file was added and is available in v3.3.16
So for my environment, upgrading top to 3.3.16 would give me the config feature you're looking for. Obviously your environment might be different.
That being said, I haven't found any way to pass this as a command argument. But with some shell scripting and sed manipulation of the config file it could be handled.
Solution 3:[3]
I use the "expect" tool to start "top", enter the filtering commands, then let top continue running normally.
#!/usr/bin/env expect
spawn top
expect "COMMAND"
send "O"
expect "add filter"
send "COMMAND=MyProcess\n"
interact
When using expect you typically must wait for some expected output before sending the next keystrokes. I chose COMMAND from the top headers to wait for top to startup, and "add filter" after sending "O" to make sure top is ready for the filtering string. The "interact" command tells expect to keep the command running and pass all input/output through unmodified until the command exits.
Expect is a tcl interpreter, so you could add support for passing command line arguments to the top command while looking for extra filtering arguments and turning those into expect/send sequences. My use case had a fixed command and I didn't need extra options for top, so a hardcoded expect script was all I needed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | glenn jackman |
| Solution 2 | |
| Solution 3 | Rudy Albachten |
