Is there any way to select the columns that the standard unix top command displays without entering interactive mode. I would like to be able to call the top command from within another C program.
Asked
Active
Viewed 178 times
1 Answers
1
See top
's command line options. -b
is for non-interactive use. Probably you will want to invoke -n
to tell it how many iterations to use.
top -b -n1 | other_program
As for choosing columns, that might require a more interactive method (like using a pseudo-tty or Expect
in Tcl
) of operating top
. Either that or collect the desired information directly from /proc/
...
As an example, look at the rich set of information available for most processes, for this example, I chose pid 1429, the firefox browser I am using to write this.
[wally@f17tb3d VMs]$ ll /proc/1429/
total 0
dr-xr-xr-x. 2 wally wally 0 Apr 4 05:58 attr
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 autogroup
-r--------. 1 wally wally 0 Apr 4 05:58 auxv
-r--r--r--. 1 wally wally 0 Apr 4 05:58 cgroup
--w-------. 1 wally wally 0 Apr 4 05:58 clear_refs
-r--r--r--. 1 wally wally 0 Mar 10 16:55 cmdline
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 comm
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 coredump_filter
-r--r--r--. 1 wally wally 0 Apr 4 05:58 cpuset
lrwxrwxrwx. 1 wally wally 0 Apr 4 05:58 cwd -> /home/wally
-r--------. 1 wally wally 0 Apr 4 05:58 environ
lrwxrwxrwx. 1 wally wally 0 Mar 23 18:37 exe -> /usr/lib64/firefox/firefox
dr-x------. 2 wally wally 0 Apr 4 05:58 fd
dr-x------. 2 wally wally 0 Apr 4 05:58 fdinfo
-r--------. 1 wally wally 0 Apr 4 05:58 io
-r--r--r--. 1 wally wally 0 Apr 4 05:58 limits
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 loginuid
-r--r--r--. 1 wally wally 0 Apr 4 05:58 maps
-rw-------. 1 wally wally 0 Apr 4 05:58 mem
-r--r--r--. 1 wally wally 0 Apr 4 05:58 mountinfo
-r--r--r--. 1 wally wally 0 Apr 4 05:58 mounts
-r--------. 1 wally wally 0 Apr 4 05:58 mountstats
dr-xr-xr-x. 5 wally wally 0 Apr 4 05:58 net
dr-x--x--x. 2 wally wally 0 Apr 4 05:58 ns
-r--r--r--. 1 wally wally 0 Apr 4 05:58 numa_maps
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 oom_adj
-r--r--r--. 1 wally wally 0 Apr 4 05:58 oom_score
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 oom_score_adj
-r--r--r--. 1 wally wally 0 Apr 4 05:58 pagemap
-r--r--r--. 1 wally wally 0 Apr 4 05:58 personality
lrwxrwxrwx. 1 wally wally 0 Apr 4 05:58 root -> /
-rw-r--r--. 1 wally wally 0 Apr 4 05:58 sched
-r--r--r--. 1 wally wally 0 Apr 4 05:58 sessionid
-r--r--r--. 1 wally wally 0 Apr 4 05:58 smaps
-r--r--r--. 1 wally wally 0 Apr 4 05:58 stack
-r--r--r--. 1 wally wally 0 Mar 10 16:56 stat
-r--r--r--. 1 wally wally 0 Apr 4 05:56 statm
-r--r--r--. 1 wally wally 0 Mar 10 16:56 status
-r--r--r--. 1 wally wally 0 Apr 4 05:58 syscall
dr-xr-xr-x. 43 wally wally 0 Mar 10 16:56 task
-r--r--r--. 1 wally wally 0 Apr 4 05:58 wchan
For example, the command line:
[wally@f17tb3d VMs]$ cat /proc/1429/cmdline
/usr/lib64/firefox/firefox[wally@f17tb3d VMs]$

wallyk
- 56,922
- 16
- 83
- 148
-
Yes, but how do I set the columns to display from within batch mode? I could not find any command line options that accomplish this. – JohnDoe Apr 04 '14 at 00:20
-
For example, from the documentation (http://unixhelp.ed.ac.uk/CGI/man-cgi?top), in section 2b it discusses how to select and order columns. Although, it only discusses how to do this in interactive mode. – JohnDoe Apr 04 '14 at 00:31
-
@JohnDoe: Sorry, I was interrupted updating my answer. I discuss solutions to that problem. – wallyk Apr 04 '14 at 00:59
-
Can you give an example using one of those methods? – JohnDoe Apr 04 '14 at 01:30