in reply to Capture Console Results?

My mac lets me specify ps -auwwx which will show you the ENTIRE command line for every process. Note, just one 'w' just gives you a 'standard wide' width result list, the extra 'w' goes the extra mile. But if you man ps on your mac, you'll see:

... -w Use 132 columns to display information, instead of the de +fault which is your window size. If the -w option is specified + more than once, ps will use as many columns as necessary witho +ut regard for your window size. ...

... this will also work on Linux (if you drop the preceeding '-' to the auwwx) and FreeBSD. Though I think I like the "pure perl" solution a bit better.

Update: I made some changes to your open and your split to make sure I wasn't leading you astray. Note that the command is "ps -auwwx" as opposed to "ps - aux" (please notice the space I have omitted) plus I'm throwing away all the rest of the bits of the results I don't care about. Also note that there are more columns returned with the modification of ps

open(PS_F, "ps -auwwx | grep inder |") || die "Can't open console\n"; while (<PS_F>) { (undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$comm +,$comm1,$comm2) = split; $comm .= ' '.$comm1.' '.$comm2; if($comm =~ m/Theservice.pl /i) { print "Service Already Running\n"; } print $comm, "\n"; } close(PS_F);

Since I don't have Theservice.pl running, I'm grepping for "inder"... the output is:

$ perl ps.pl /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder -psn_0_9 +17505 sh -c ps grep inder

HTH



--chargrill
$/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );

Replies are listed 'Best First'.
Re^2: Capture Console Results?
by acid06 (Friar) on Feb 25, 2006 at 00:58 UTC
    Just a side note really, but I think that

    (undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$comm,$comm1,$comm2) = split;

    is probably best written as

    ($comm, $comm1, $comm2) = (split)[10..12];


    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="

      True, but I made the assumption he cared about some of the columns as he went through the trouble of creating variables to store them (though the example script makes no use of them). aundef,<esc>l8. in vi (to repeat adding more 'undef,') was easier than typing out new variable names, plus gives a hint that there are more columns of output than the original command provides, and could therefore be turned back into variables should the OP so desire.



      --chargrill
      $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
      Actually I do care about the variables that come before it, but I didn't know that you could even do what you're suggesting, so thanks!!
Re^2: Capture Console Results?
by ecuguru (Monk) on Feb 26, 2006 at 05:35 UTC
    Thanks for the -ww tip. Now I see the entire line wrapped, but depending on the window size, the same problem still exists with -ww. Although it does show the entire line (wrappped), if the window has a size that happens to cause the word you're searching for to split between the lines, then the code doesn't find it because your search word has a \n in it not matching the grep. Rather than doing a foreach, I'll get rid of the \ns, and do a search for the string from the entire result, rather than going line by line. That should work (will update either way).
      That didn't work. The console, if the window is a certain size, will wrap the lines but it actually appears to cutoff part of the line (even with the ww added). Can I force resize a window, tell the system to pretend my console is a certain size and not wrap the lines, or is there any other way to disregard the size of the window in the script and get the whole output dumped to memory somewhere?
      28066 p3 S+ 353:01.11 perl /Library/WebServer/Documents/perlscr +ipts/refresh/getDat ##This is supposed to be getDataLive.pl script + i want to monitor. 571 std S+ 0:00.05 perl systemcmd.pl ##The name of my script +file 590 std S+ 0:00.02 sh -c ps - auwwx | grep perl 592 std R+ 0:00.00 grep perl
      Wondering if I'm making this harder than it has to be. Would like to keep from using launchd, not all the mac platform use launchd for services.
      Current code below
      open(PS_F, "ps - auwwx | grep perl |") || die "Can't open cons +ole\n"; my @data = <PS_F>; close(PS_F); my $res_data = join(" ",@data); print "Res data is $res_data\n---\n"; my $comm = $res_data; $comm =~ s/\n//g; $comm =~ s/\r//g; if($comm =~ m/getDataLive.pl/i) {

        I notice you still have the space between the dash (-) and the "auwwx". My test code refuses to find "inder" when I leave the space between the dash and the auwwx (and I know for CERTAIN that I'm currently running the Finder), but seems to find it just fine with a standard width (80 chars) window, again when I've shrank my window to 35 chars wide, all the while the output of the command in Terminal is clearly beyond that width:

        My-Computer()$ ps -auwwx | grep inder chargrill 255 0.0 2.2 228420 23320 ?? S 16Feb06 1:44.33 + /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder -psn_0 +_917505 chargrill 23153 0.0 0.0 27820 4 p2 R+ 12:08PM 0:00.00 + grep inder My-Computer()$ ps - auwwx | grep inder 23155 p2 R+ 0:00.00 grep inder My-Computer()$

        When I type "ps - auwwx", I get nothing like what I expect to see, which is an extra wide listing of all processes from all users running on the computer, instead I see (apparently) only the login (interactive) processes currently running as my user:

        My-Computer()$ ps - auwwx PID TT STAT TIME COMMAND 12006 p1 S 0:00.10 -bash 18744 p1 S+ 0:00.22 /usr/bin/perl /usr/bin/perldoc perlop 18753 p1 S+ 0:00.02 /usr/bin/less /tmp/Zf90IVbUVV 12073 p2 S 0:00.69 -bash 3319 p3 Ss+ 0:00.09 -bash 15497 p4 S 0:00.14 -bash 20550 p4 S+ 0:00.21 /usr/bin/perl /usr/bin/perldoc perlvar 20559 p4 S+ 0:00.06 /usr/bin/less /tmp/rTkTg152OS

        Vs.

        ls-Computer()$ ps -auwwx | more USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME C +OMMAND chargrill 936 6.6 1.6 222620 17208 ?? S 16Feb06 21:48.75 + /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_ +5898241 chargrill 18190 4.1 15.4 477220 161736 ?? S Tue03PM 129:48.52 + /Applications/Safari.app/Contents/MacOS/Safari -psn_0_71303169 windowse 103 3.6 4.0 257112 41960 ?? Ss 16Feb06 352:48.88 +/System/Library/Frameworks/ApplicationServices.framework/Frameworks/C +oreGraphics.framework/Resources/WindowServer -daemon ---<SNIP>---

        Please try it getting rid of the space, I beg of you! :-) I've never seen that particular incarnation of command line options to ps. Either with the dash immediately preceeding the options, or leaving the dash out (Linux will complain about a superfluous dash). But not ps_-_auwwx.



        --chargrill
        $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );