problems in redirecting TOP

Hi All,
I am trying to run top in crontab, but I am getting problem while trying to redirecting the output of top cmd.
I am getting the process part but the CPU states, user, kernel, iowait, swap part is not showing.

While trying top > some_file_name the output i'm getting,

top: cannot open /dev/kmem
kvm_open: Permission denied

Is that the problem or there is any other way i can get that data directed as well.

Thanks,
Ran

I take it you don't have any problems running from command-line?

top:

-bash-3.00$ top -d 1 > /tmp/top.lst
-bash-3.00$ cat /tmp/top.lst
load averages: 0.28, 0.39, 0.40 14:58:39
42 processes: 41 sleeping, 1 on cpu

Memory: 16G real, 9929M free, 6067M swap in use, 12G swap free

PID USERNAME LWP PRI NICE SIZE RES STATE TIME CPU COMMAND
18827 cquest 115 26 11 0K 0K sleep 77:32 2.66% java
24109 jmcontre 1 59 0 3784K 1536K cpu/1 0:00 0.02% top
24085 jmcontre 1 49 0 3000K 2392K sleep 0:00 0.02% bash
24083 jmcontre 1 59 0 8232K 2736K sleep 0:00 0.02% sshd
99 root 6 59 0 3896K 3088K sleep 0:44 0.00% picld
627 root 1 100 -20 2304K 1552K sleep 0:37 0.00% xntpd
24080 root 1 59 0 8448K 4864K sleep 0:00 0.00% sshd
7 root 13 59 0 8864K 7848K sleep 0:24 0.00% svc.startd
113 root 25 59 0 3856K 2936K sleep 0:23 0.00% nscd
9 root 16 59 0 9648K 8696K sleep 0:12 0.00% svc.configd
1 root 1 59 0 2392K 1392K sleep 0:08 0.00% init
220 root 4 59 0 4792K 3816K sleep 0:07 0.00% inetd
207 root 1 60 0 2696K 1336K sleep 0:07 0.00% cron
354 root 12 59 0 5712K 5016K sleep 0:06 0.00% fmd
535 root 1 59 0 0K 0K sleep 0:04 0.00% snmpd

prstat:

-bash-3.00$ prstat 1 1 > /tmp/top.lst
-bash-3.00$ cat /tmp/top.lst
PID USERNAME SIZE RSS STATE PRI NICE TIME CPU PROCESS/NLWP
18827 cquest 6154M 5590M sleep 26 11 1:17:35 1.6% java/115
24111 jmcontre 1624K 1272K cpu1 49 0 0:00:00 0.0% prstat/1
24085 jmcontre 3000K 2392K sleep 49 0 0:00:00 0.0% bash/1
24083 jmcontre 8232K 2736K sleep 59 0 0:00:00 0.0% sshd/1
627 root 2304K 1552K sleep 100 - 0:00:37 0.0% xntpd/1
99 root 3896K 3088K sleep 59 0 0:00:44 0.0% picld/6
223 root 2136K 1440K sleep 59 0 0:00:00 0.0% ttymon/1
209 daemon 2608K 1528K sleep 59 0 0:00:00 0.0% rpcbind/1
224 root 2104K 1360K sleep 59 0 0:00:00 0.0% ttymon/1
212 daemon 2656K 1936K sleep 59 0 0:00:00 0.0% statd/1
323 root 4600K 2208K sleep 59 0 0:00:00 0.0% syslogd/19
131 root 2720K 1936K sleep 59 0 0:00:00 0.0% devfsadm/6
126 root 2344K 1360K sleep 59 0 0:00:00 0.0% powerd/3
213 root 2000K 1248K sleep 59 0 0:00:00 0.0% sac/1
207 root 2696K 1336K sleep 60 0 0:00:07 0.0% cron/1
Total: 42 processes, 271 lwps, load averages: 0.17, 0.34, 0.38

That didn't work, the problem i mentioned still persistes.

Seems like permisson issue. Try running as root and see if it works..

Doesn't looks like a permission issue, because when i run it from the cmd prompt, it is apperaring fine. Problem is occuring only in case of redirecing.

Actually it does look like a permission problem. What happens when you do this:

top -n 1 2> /tmp/top_errors
cat /tmp/top_errors

I'm guessing /tmp/top_errors contains:
top: cannot open /dev/kmem
kvm_open: Permission denied

No need to worry about that, top has other ways of getting the information it needs, but if you really want to grab that as well

top > some file 2>&1

Dear Friend,

Type the following in a shell script to get the top output.

top -d 8 -s 600 >> pvpk_240706.top

Explanation:
It keeps appending top into a file pvpk_240706.top

Option d : Number of Displays. In this example you get 8 Top displays. with
10 top process.

Option s: Number of SECONDS to delay for each display.

You need to ignore the first output as it doesnt capture the CPU states.

I am sure this is what you were looking for.

With warm regards,
Pavan Kumar.

1 Like

Excellent! thats what exactly i'm looking for. Thaks Pavan.