Showing Running Processes in variable

Discussion in 'Ruby' started by jackster the jackle, Dec 13, 2007.

  1. I want to capture the list of running processes on my computer. I am to
    get all the data into my variable with a simple system call:

    x = `ps -el`

    My problem comes when I want to display the data back out in a legible
    format that makes sense.

    What is the best way to do this?

    thanks

    jackster
    --
    Posted via http://www.ruby-forum.com/.
     
    jackster the jackle, Dec 13, 2007
    #1
    1. Advertising

  2. jackster the jackle

    darren kirby Guest

    quoth the jackster the jackle:
    > I want to capture the list of running processes on my computer. I am to
    > get all the data into my variable with a simple system call:
    >
    > x = `ps -el`
    >
    > My problem comes when I want to display the data back out in a legible
    > format that makes sense.
    >
    > What is the best way to do this?


    Not sure if this is the best but:

    x.split("\n").each do |line|
    puts line
    end

    works for me.

    > thanks
    >
    > jackster


    -d
    --
    darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
    "...the number of UNIX installations has grown to 10, with more expected..."
    - Dennis Ritchie and Ken Thompson, June 1972
     
    darren kirby, Dec 13, 2007
    #2
    1. Advertising

  3. Thanks for the help Darren....what you proposed works but it doesn't
    print out the processes in a viewable table in rails....I think I
    somehow need to add a "\n" or a line break to the result....This might
    be a rails question now but just in case you might know, here is what I
    have in my controller:

    def processes
    x=`ps -el`
    y=x.split("\n")
    y.each do |line|
    end
    return y
    end

    Then I simply display the variable in my view with: <%= processes() %>

    Here is what the view looks like:

    F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD000 S 2202 11254 1 0
    75 0 - 545 wait4 ? 00:00:00 mysqld_sa000 S 2202 11342 11254 0 80 5 -
    5887 schedu ? 00:00:00 mysqld040 S 2202 11368 11342 0 80 5 - 5887 schedu
    ? 00:00:01 mysqld040 S 2202 11370 11368 0 80 5 - 5887 rt_sig ? 00:00:00
    mysqld000 S 2202 22812 1 0 75 0 - 5744 schedu ? 00:00:00 dispatch.000 S
    2202 23955 1 0 75 0 - 5322 schedu ? 00:00:00 dispatch.100 S 2202 23807
    23691 0 75 0 - 579 schedu pts/4 00:00:00 sh000 T 2202 26309 23807 0 75 0
    - 1137 do_sig pts/4 00:00:00 irb040 S 2202 18127 1 2 75 0 - 6578 schedu
    ? 00:00:00 httpd040 S 2202 18132 18127 0 75 0 - 6299 schedu ? 00:00:00
    httpd040 S 2202 18172 18127 0 75 0 - 6605 schedu ? 00:00:00 httpd000 S
    2202 18178 18132 17 79 0 - 5475 pipe_w ? 00:00:00 dispatch.040 S 2202
    18185 18127 0 76 0 - 6581 schedu ? 00:00:00 httpd000 R 2202 18218 18178
    0 80 0 - 397 - ? 00:00:00 ps100 S 2202 18219 1475 0 77 0 - 503 wait4 ?
    00:00:00 sps000 R 2202 18235 18219 0 78 0 - 897 - ? 00:00:00 ps

    Thanks again,

    jackster

    darren kirby wrote:
    > quoth the jackster the jackle:
    >> I want to capture the list of running processes on my computer. I am to
    >> get all the data into my variable with a simple system call:
    >>
    >> x = `ps -el`
    >>
    >> My problem comes when I want to display the data back out in a legible
    >> format that makes sense.
    >>
    >> What is the best way to do this?

    >
    > Not sure if this is the best but:
    >
    > x.split("\n").each do |line|
    > puts line
    > end
    >
    > works for me.
    >
    >> thanks
    >>
    >> jackster

    >
    > -d


    --
    Posted via http://www.ruby-forum.com/.
     
    jackster the jackle, Dec 13, 2007
    #3
  4. jackster the jackle

    Adam Shelly Guest

    On 12/13/07, jackster the jackle <> wrote:
    > Thanks for the help Darren....what you proposed works but it doesn't
    > print out the processes in a viewable table in rails....I think I
    > somehow need to add a "\n" or a line break to the result....This might


    I'm not a web guy, but... You need a <br> to generate a newline in html, right?

    > be a rails question now but just in case you might know, here is what I
    > have in my controller:
    >
    > def processes
    > x=`ps -el`
    > y=x.split("\n")
    > y.each do |line|
    > end
    > return y
    > end
    >


    The each statement does nothing there, and the return is redundant.
    How about just

    def processes
    `ps -el`.gsub(/\n/,"<br>")
    end


    -Adam
     
    Adam Shelly, Dec 13, 2007
    #4
  5. jackster the jackle wrote:
    > I want to capture the list of running processes on my computer. I am to
    > get all the data into my variable with a simple system call:
    >
    > x = `ps -el`
    >
    > My problem comes when I want to display the data back out in a legible
    > format that makes sense.
    >
    > What is the best way to do this?


    I recommend sys-proctable instead.

    require 'sys/proctable'
    include Sys

    ProcTable.ps do |process|
    p process.cmdline
    ...
    end

    Regards,

    Dan
     
    Daniel Berger, Dec 13, 2007
    #5
  6. jackster the jackle

    Joon You Guest

    Try this:

    def view
    @result = ""
    x = `ps -el`
    x.each_line do |l|
    @result += l + "<br />"
    end
    return @result
    end

    I remembered that carriage return is always funny from system to system.
    This should work.

    Good luck!

    jackster the jackle wrote:
    > I want to capture the list of running processes on my computer. I am to
    > get all the data into my variable with a simple system call:
    >
    > x = `ps -el`
    >
    > My problem comes when I want to display the data back out in a legible
    > format that makes sense.
    >
    > What is the best way to do this?
    >
    > thanks
    >
    > jackster


    --
    Posted via http://www.ruby-forum.com/.
     
    Joon You, Dec 13, 2007
    #6
  7. Thanks for the replies but they don't seem to work in Rails.

    Dan,

    Is sys/proctable a gem that I can install? ..if so, do you have link?

    thanks

    jackster

    Daniel Berger wrote:
    > jackster the jackle wrote:
    >> I want to capture the list of running processes on my computer. I am to
    >> get all the data into my variable with a simple system call:
    >>
    >> x = `ps -el`
    >>
    >> My problem comes when I want to display the data back out in a legible
    >> format that makes sense.
    >>
    >> What is the best way to do this?

    >
    > I recommend sys-proctable instead.
    >
    > require 'sys/proctable'
    > include Sys
    >
    > ProcTable.ps do |process|
    > p process.cmdline
    > ...
    > end
    >
    > Regards,
    >
    > Dan


    --
    Posted via http://www.ruby-forum.com/.
     
    jackster the jackle, Dec 13, 2007
    #7
  8. jackster the jackle

    Joon You Guest

    I just tested it in Rails 2.0.1 with the following:

    controller:

    def view
    @result = ""
    x = `ps -el`
    x.each_line do |l|
    @result += l + "<br />"
    end
    return @result
    end

    model:

    <= @result %>

    What version of Rails are you using?

    jackster the jackle wrote:
    > Thanks for the replies but they don't seem to work in Rails.
    >
    > Dan,
    >
    > Is sys/proctable a gem that I can install? ..if so, do you have link?
    >
    > thanks
    >
    > jackster
    >
    > Daniel Berger wrote:
    >> jackster the jackle wrote:
    >>> I want to capture the list of running processes on my computer. I am to
    >>> get all the data into my variable with a simple system call:
    >>>
    >>> x = `ps -el`
    >>>
    >>> My problem comes when I want to display the data back out in a legible
    >>> format that makes sense.
    >>>
    >>> What is the best way to do this?

    >>
    >> I recommend sys-proctable instead.
    >>
    >> require 'sys/proctable'
    >> include Sys
    >>
    >> ProcTable.ps do |process|
    >> p process.cmdline
    >> ...
    >> end
    >>
    >> Regards,
    >>
    >> Dan


    --
    Posted via http://www.ruby-forum.com/.
     
    Joon You, Dec 13, 2007
    #8
  9. jackster the jackle wrote:
    > Thanks for the replies but they don't seem to work in Rails.
    >
    > Dan,
    >
    > Is sys/proctable a gem that I can install? ..if so, do you have link?


    gem install sys-proctable

    Or, you can grab the source at http://www.rubyforge.org/projects/sysutils

    Regards,

    Dan
     
    Daniel Berger, Dec 13, 2007
    #9
  10. Thanks for all the help everyone. Someone from the rails forum gave me
    what seems like the simplest solution...here's what he wrote:

    The problem you have is an HTML one, not a ruby one.

    Basically, HTML ignores a \n unless it is inside a <pre> </pre> tag.

    So, you can do this instead:

    <pre>
    <%= processes %>
    </pre>
    And it should work fine.

    Also your Ruby code can be just:

    def processes
    `ps -el`
    end

    To get the same result. You don't need all that splitting or
    assignments or even return command.

    ----------------

    thanks again for all the good ideas.

    jackster


    --
    Posted via http://www.ruby-forum.com/.
     
    jackster the jackle, Dec 14, 2007
    #10
  11. jackster the jackle wrote:
    > I want to capture the list of running processes on my computer. I am to
    > get all the data into my variable with a simple system call:
    >
    > x = `ps -el`
    >
    > My problem comes when I want to display the data back out in a legible
    > format that makes sense.
    >
    > What is the best way to do this?
    >
    > thanks
    >
    > jackster


    You haven't specified what operating system you're using. On *Linux*,
    the "ps" command generates fields separated by whitespace, so some
    simple regex operations will work on them. Other "Unix-like" operating
    systems will not necessarily behave this way.
     
    M. Edward (Ed) Borasky, Dec 15, 2007
    #11
  12. jackster the jackle

    John Joyce Guest

    On Dec 13, 2007, at 6:52 PM, jackster the jackle wrote:

    > Thanks for all the help everyone. Someone from the rails forum gave me
    > what seems like the simplest solution...here's what he wrote:
    >
    > The problem you have is an HTML one, not a ruby one.
    >
    > Basically, HTML ignores a \n unless it is inside a <pre> </pre> tag.
    >
    > So, you can do this instead:
    >
    > <pre>
    > <%= processes %>
    > </pre>
    > And it should work fine.
    >
    > Also your Ruby code can be just:
    >
    > def processes
    > `ps -el`
    > end
    >
    > To get the same result. You don't need all that splitting or
    > assignments or even return command.

    While using <pre> elements to preserve whitespace is simple it also
    is not entirely consistent.
    Remember you are dealing with different user-agents (browsers) on
    different platforms.
    Particularly, tab characters (that may be generated by some command-
    line tool output) may be displayed at different widths depending on
    the user-agent and/or the system.
    the <pre> element is a useful one but its implementations will not be
    as predictable for rendering by the user-agent as going back and
    breaking that output into some parts to populate some kind of table
    or div structure.

    Some tools do not produce \n endings even, just relying on terminal
    window for line-wrapping...
     
    John Joyce, Dec 21, 2007
    #12
  13. On Dec 14, 9:41=A0pm, "M. Edward (Ed) Borasky" <>
    wrote:

    <snip>

    > You haven't specified what operating system you're using. On *Linux*,
    > the "ps" command generates fields separated by whitespace, so some
    > simple regex operations will work on them. Other "Unix-like" operating
    > systems will not necessarily behave this way.


    This is why you should use sys-proctable. :)

    Regards,

    Dan
     
    Daniel Berger, Dec 21, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Divyang M
    Replies:
    4
    Views:
    1,055
    Divyang M
    Feb 17, 2005
  2. Rusty Shackleford

    Worker Processes Running counter?

    Rusty Shackleford, Oct 5, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    423
    Rusty Shackleford
    Oct 5, 2005
  3. Ray Booysen
    Replies:
    2
    Views:
    666
    Ray Booysen
    Mar 28, 2006
  4. Jeff Rodriguez
    Replies:
    23
    Views:
    1,207
    David Schwartz
    Dec 9, 2003
  5. Marc Heiler
    Replies:
    1
    Views:
    192
    Robert Klemme
    May 24, 2009
Loading...

Share This Page