I'm trying to write a simple top tool to view httpd children. I'd like to use the Curses library for this. I'm having a problem refreshing/reprinting the display after a two second pause. Can someone offer help?
#!/usr/bin/perl use strict; use Curses; while (1) { open PS_TREE, 'ps -eo pid,ppid,uid,rss,vsize,pcpu,pmem,time,stime, +cmd -ww --sort=pid | ' or die $!; my %ps_tree; while (my $line = <PS_TREE>) { chomp $line; my ( $pid, $ppid, $uid, $rss, $vsz, $cpu, $mem, $rtime, $stime, @cmd ) = split / +/, $line; $ps_tree{$pid} = { ppid => $ppid, uid => $uid, rss => $rss, vsz => $vsz, cpu => $cpu, mem => $mem, rtime => $rtime, stime => $stime, cmd => join ' ', @cmd }; } my @httpd_pids; for my $pid (keys %ps_tree) { if ($ps_tree{$pid}{cmd} =~ /httpd/) { push @httpd_pids, $pid; } } my @httpd_kids; for my $pid (keys %ps_tree) { if ($ps_tree{$pid}{cmd} !~ /httpd/ && grep { $_ == $ps_tree{$pid}{ppid} } @httpd_pids) { push @httpd_kids, $pid; } } my $line_cntr = 0; my $hdr = sprintf "%8s", 'PID'; $hdr .= sprintf "%8s", 'PPID'; $hdr .= sprintf "%8s", 'UID'; $hdr .= sprintf "%8s", 'RSS'; $hdr .= sprintf "%8s", 'VSZ'; $hdr .= sprintf "%8s", '%CPU'; $hdr .= sprintf "%8s", '%MEM'; $hdr .= sprintf "%10s", 'TIME'; $hdr .= sprintf "%10s", 'STIME'; $hdr .= sprintf "%14s\n", 'CMD'; initscr(); attron(A_STANDOUT); addstr($line_cntr++, 0, $hdr); attroff(A_STANDOUT); for my $kid (@httpd_kids) { my $line = sprintf "%8s", $kid; $line .= sprintf "%8s", $ps_tree{$kid}{ppid}; $line .= sprintf "%8s", $ps_tree{$kid}{uid}; $line .= sprintf "%8s", $ps_tree{$kid}{rss}; $line .= sprintf "%8s", $ps_tree{$kid}{vsz}; $line .= sprintf "%8s", $ps_tree{$kid}{cpu}; $line .= sprintf "%8s", $ps_tree{$kid}{mem}; $line .= sprintf "%10s", $ps_tree{$kid}{rtime}; $line .= sprintf "%10s", $ps_tree{$kid}{stime}; $line .= sprintf "%14s\n", $ps_tree{$kid}{cmd}; addstr($line_cntr++, 0, $line); } sleep 2; erase(); refresh(); move(0, 0); } __END__

In reply to Top like display with Curses by mhearse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.