Processes in Linux from Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johny

    Processes in Linux from Python

    To get a number of the http processes running on my Linux( Debia box)
    I use
    ps -ef | grep "[h]ttpd" | wc -l

    But If I want to use to get a number of the http processes from my
    Python program I must use a popen command e.g.
    popen2.popen3(' ps -ef | grep "[h]ttpd" | wc -l')
    that is I must call an external command from Python.

    But it creates a zombie.
    So my question is:
    Is it possible to get a number of the http processes running on Linux
    directly from Python ?

    Thanks for help.
    L.
  • Diez B. Roggisch

    #2
    Re: Processes in Linux from Python

    Johny schrieb:
    To get a number of the http processes running on my Linux( Debia box)
    I use
    ps -ef | grep "[h]ttpd" | wc -l
    >
    But If I want to use to get a number of the http processes from my
    Python program I must use a popen command e.g.
    popen2.popen3(' ps -ef | grep "[h]ttpd" | wc -l')
    that is I must call an external command from Python.
    The shell does the exact same thing. And by the way: i think you miss a

    grep -v grep

    after the first grep - otherwise you count the grep itself.
    But it creates a zombie.
    Try using the subprocess module. It should not require you to call wait
    yourself.
    So my question is:
    Is it possible to get a number of the http processes running on Linux
    directly from Python ?
    Not really, or at least not with less effort & without duplicating
    system tools functionality.

    Diez

    Comment

    • Christian Heimes

      #3
      Re: Processes in Linux from Python

      Johny wrote:
      Is it possible to get a number of the http processes running on Linux
      directly from Python ?
      The Python core has no direct API for the job. However you can use the
      virtual /proc/ file system for the job. Or check out my enumprocess
      package.

      It's far from perfect but it should do the job on Linux.

      Christian

      Comment

      • Wojtek Walczak

        #4
        Re: Processes in Linux from Python

        On Sun, 31 Aug 2008 23:25:56 -0700 (PDT), Johny wrote:
        To get a number of the http processes running on my Linux( Debia box)
        I use
        ps -ef | grep "[h]ttpd" | wc -l
        ....
        So my question is:
        Is it possible to get a number of the http processes running on Linux
        directly from Python ?
        Yes, it is. There is a number of third party packages that provide
        such functionality, including PSI: http://www.psychofx.com/psi/
        I never actually liked them, because they are parsing /proc directory
        by themselves. Thus I wrote my own tool that is a wrapper around
        procps library (libproc* in your /lib, probably). Your system tools
        like ps, w or top are using this library. My wrapping library is
        available at: http://code.google.com/p/procpy/
        In your case you could use it like this:
        >>import procpy
        >>pp = procpy.Proc()
        >>for pid in pp.pids:
        .... if pp.procs[pid]['cmd'] == 'apache2':
        .... print pp.procs[pid]['tid']
        ....
        5204
        5205
        5206
        5208
        >>>
        it prints the PIDs of all the apache2 processes on my system.
        Procpy is fine for my own needs, but if I were about to write
        a code that is intended to be portable, I'd use PSI. It also is
        more mature.

        --
        Regards,
        Wojtek Walczak,
        Cena domeny: 4999 PLN (do negocjacji). Możliwość kupna na raty od 624.88 PLN miesięcznie. Oferta sprzedaży znajduje się w serwisie Aftermarket.pl, największej giełdzie domen internetowych w Polsce.

        Comment

        • Derek Martin

          #5
          Re: Processes in Linux from Python

          On Mon, Sep 01, 2008 at 08:40:42AM +0200, Diez B. Roggisch wrote:
          Johny schrieb:
          To get a number of the http processes running on my Linux( Debia box)
          I use
          ps -ef | grep "[h]ttpd" | wc -l
          [...]
          The shell does the exact same thing. And by the way: i think you miss a
          >
          grep -v grep
          Indeed not. The brackets around the 'h' (which make it a character
          class, or range if you prefer) prevent the regex from matching itself.

          --
          Derek D. Martin

          GPG Key ID: 0x81CFE75D


          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.1 (GNU/Linux)

          iD8DBQFIvK1ydjd lQoHP510RAmDHAJ 4yxEcKkE3zOd+Ei kybHtvIk+r/wACeOVIq
          eFRLkEJgzX3W9kG nWVnAP5A=
          =9ZfI
          -----END PGP SIGNATURE-----

          Comment

          Working...