getting a process's PID

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

    getting a process's PID

    Hello,

    I am trying to get python to give me the PID of a process (in this case
    HUB). I have it working, except for the fact that the output includes
    \012 (newline). Is there a way to ask python not to give me a newline?

    Python 1.4 (Oct 14 1997) [C]
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >>import os
    >>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
    >>h = g.readlines()
    >>g.close()
    >>h
    ['87334\012']

    Thanks in advanced for any guidance.



    --
    Randomly generated signature
    Whoever said nothing is impossible never tried slamming a revolving door.
  • Erik Johnson

    #2
    Re: getting a process's PID

    "eldorado" <eldorado@io.co mwrote in message
    news:2006122710 2939.L20663@eri s.io.com...
    Hello,
    >
    I am trying to get python to give me the PID of a process (in this case
    HUB). I have it working, except for the fact that the output includes
    \012 (newline). Is there a way to ask python not to give me a newline?
    >
    Python 1.4 (Oct 14 1997) [C]
    Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
    >import os
    >g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
    >h = g.readlines()
    >g.close()
    >h
    ['87334\012']

    There's more than one way to do it! (Oh, sorry, that's Perl...)

    The two most standard ways would be to call strip() on your string to get
    one sans both leading and trialing whitespace

    print h.strip()

    or if you know exactly what you've got (i.e., the newline you don't want is
    just the last character), you can just get rid of it:

    h = h[:-1]


    HTH,
    -ej


    Comment

    • Duncan Booth

      #3
      Re: getting a process's PID

      "Erik Johnson" <ej at wellkeeper dot comwrote:
      There's more than one way to do it! (Oh, sorry, that's Perl...)
      >
      The two most standard ways would be to call strip() on your string to
      get one sans both leading and trialing whitespace
      >
      print h.strip()
      >
      or if you know exactly what you've got (i.e., the newline you don't
      want is just the last character), you can just get rid of it:
      >
      h = h[:-1]
      Or if you don't know for sure it's there, but don't want to lose other
      whitespace:

      print h.rstrip('\n')

      Comment

      • eldorado

        #4
        Re: getting a process's PID

        On Wed, 27 Dec 2006, Erik Johnson wrote:
        "eldorado" <eldorado@io.co mwrote in message
        news:2006122710 2939.L20663@eri s.io.com...
        >Hello,
        >>
        >I am trying to get python to give me the PID of a process (in this case
        >HUB). I have it working, except for the fact that the output includes
        >\012 (newline). Is there a way to ask python not to give me a newline?
        >>
        >Python 1.4 (Oct 14 1997) [C]
        >Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
        >>>>import os
        >>>>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
        >>>>h = g.readlines()
        >>>>g.close()
        >>>>h
        >['87334\012']
        >
        >
        There's more than one way to do it! (Oh, sorry, that's Perl...)
        >
        The two most standard ways would be to call strip() on your string to get
        one sans both leading and trialing whitespace
        >
        print h.strip()
        >
        or if you know exactly what you've got (i.e., the newline you don't want is
        just the last character), you can just get rid of it:
        >
        h = h[:-1]
        >
        Thanks for the help, however it doesnt look like those two solutions quite
        work:


        >>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
        >>h = g.readlines()
        >>g.close()
        >>h
        ['87334\012']
        >>h = h[:-1]
        >>h
        []
        >>>
        >>import string
        >>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
        >>h = g.readlines()
        >>g.close()
        >>print h.strip()
        File "<stdin>", line 1
        print h.strip()
        ^
        SyntaxError: invalid syntax

        I looked up the syntax for print and it looks correct (at least to me ;)


        --
        Randomly generated signature
        You can go anywhere you want if you look serious and carry a clipboard.

        Comment

        • Erik Johnson

          #5
          Re: getting a process's PID


          "eldorado" <eldorado@io.co mwrote in message
          news:2006122711 3158.J21223@eri s.io.com...
          >g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
          >h = g.readlines()
          >g.close()
          >h
          ['87334\012']
          >h = h[:-1]
          >h
          []
          Oh, sorry... h is a list here because you are using readlines().
          I am used to doing this:

          fd = os.popen('ps -ef | grep python')
          s = fd.read()

          to get a single string. You can do something like

          s = h[0]

          and then operate on s, or you can use read() in place of readlines() to get
          h as a single string, or you can operate on the first element of h:
          >>h = ['87334\012']
          >>h[0][:-1]
          '87334'
          >>h[0].rstrip('\n')
          '87334'

          All the error handling to do in the case where you actually have multiple
          processes being matched is up to you. ;)

          -ej


          Comment

          • eldorado

            #6
            Re: getting a process's PID

            On Wed, 27 Dec 2006, Erik Johnson wrote:
            >
            "eldorado" <eldorado@io.co mwrote in message
            news:2006122711 3158.J21223@eri s.io.com...
            >
            >>>>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
            >>>>h = g.readlines()
            >>>>g.close()
            >>>>h
            >['87334\012']
            >>>>h = h[:-1]
            >>>>h
            >[]
            >
            Oh, sorry... h is a list here because you are using readlines().
            I am used to doing this:
            >
            fd = os.popen('ps -ef | grep python')
            s = fd.read()
            >
            to get a single string. You can do something like
            >
            s = h[0]
            >
            and then operate on s, or you can use read() in place of readlines() to get
            h as a single string, or you can operate on the first element of h:
            >
            >>>h = ['87334\012']
            >>>h[0][:-1]
            '87334'
            >>>h[0].rstrip('\n')
            '87334'
            >
            All the error handling to do in the case where you actually have multiple
            processes being matched is up to you. ;)
            Erik,
            Thank you very much. Works perfect. I am now off to work on the multiple
            process issue.


            --
            Randomly generated signature
            Claiming that your operating system is the best in the world because more people use it is like saying McDonalds makes the best food in the world.

            Comment

            • Sebastian 'lunar' Wiesner

              #7
              Re: getting a process's PID

              eldorado <eldorado@io.co mtyped
              Hello,
              >
              I am trying to get python to give me the PID of a process (in this
              case
              HUB). I have it working, except for the fact that the output includes
              \012 (newline). Is there a way to ask python not to give me a
              newline?
              >
              Python 1.4 (Oct 14 1997) [C]
              Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
              >>>import os
              >>>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2
              >>>}'") h = g.readlines()
              >>>g.close()
              >>>h
              ['87334\012']
              >
              Thanks in advanced for any guidance.
              Well, you could do everything in python itself, without using grep and
              awk at all:
              >>>g = os.popen("ps -e -o pid,command")
              >>>for line in g.readlines():
              >>> if 'HUB' in line:
              >>> pid = line.strip().sp lit(' ')[0]
              >>> break
              >>>print pid
              --
              Freedom is always the freedom of dissenters.
              (Rosa Luxemburg)

              Comment

              • eldorado

                #8
                Re: getting a process's PID

                On Wed, 27 Dec 2006, Sebastian 'lunar' Wiesner wrote:
                eldorado <eldorado@io.co mtyped
                >
                >Hello,
                >>
                >I am trying to get python to give me the PID of a process (in this
                >case
                >HUB). I have it working, except for the fact that the output includes
                >\012 (newline). Is there a way to ask python not to give me a
                >newline?
                >>
                >Python 1.4 (Oct 14 1997) [C]
                >Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
                >>>>import os
                >>>>g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2
                >>>>}'") h = g.readlines()
                >>>>g.close()
                >>>>h
                >['87334\012']
                >>
                >Thanks in advanced for any guidance.
                >
                Well, you could do everything in python itself, without using grep and
                awk at all:
                >
                >>>>g = os.popen("ps -e -o pid,command")
                >>>>for line in g.readlines():
                >>>> if 'HUB' in line:
                >>>> pid = line.strip().sp lit(' ')[0]
                >>>> break
                >>>>print pid
                >
                This looks cleaner than the way I was going. I created a file
                called ps.py

                #!/usr/local/bin/python
                import os
                g = os.popen("ps -e -o pid,command")
                for line in g.readlines():
                if 'HUB' in line:
                pid = line.strip().sp lit(' ')[0]
                break
                print pid

                When I run ps.py I get the following error.

                Traceback (innermost last):
                File "./ps.py", line 5, in ?
                if 'HUB' in line:
                TypeError: string member test needs char left operand

                I googled this error, but wasn't smart enough to figure out exactly what
                it means.

                --
                Randomly generated signature
                On the other hand, the early worm gets eaten.

                Comment

                • Sebastian 'lunar' Wiesner

                  #9
                  Re: getting a process's PID

                  eldorado <eldorado@io.co mtyped

                  [snip]
                  This looks cleaner than the way I was going. I created a file
                  called ps.py
                  >
                  #!/usr/local/bin/python
                  import os
                  g = os.popen("ps -e -o pid,command")
                  for line in g.readlines():
                  if 'HUB' in line:
                  pid = line.strip().sp lit(' ')[0]
                  break
                  print pid
                  >
                  When I run ps.py I get the following error.
                  >
                  Traceback (innermost last):
                  File "./ps.py", line 5, in ?
                  if 'HUB' in line:
                  TypeError: string member test needs char left operand
                  Strange!? On my system with Python 2.4 I don't get this error. It is
                  likely to be a problem of your really ancient python version. Do I
                  guess correctly from your previous postings, that you're still using
                  version 1.4?.

                  The only thing, you could advice you to, is to replace the line with the
                  following code, which should do very much the same thing:
                  >>>if line.find('HUB' ) -1:
                  If you are really using version 1.4, you should replace the next line,
                  too, because string methods came after 1.4:
                  >>>pid = string.split(st ring.strip(line ), ' ')[0]
                  Bye
                  Sebastian Wiesner

                  --
                  Freedom is always the freedom of dissenters.
                  (Rosa Luxemburg)

                  Comment

                  • eldorado

                    #10
                    Re: getting a process's PID

                    On Wed, 27 Dec 2006, Sebastian 'lunar' Wiesner wrote:
                    eldorado <eldorado@io.co mtyped
                    >
                    Strange!? On my system with Python 2.4 I don't get this error. It is
                    likely to be a problem of your really ancient python version. Do I
                    guess correctly from your previous postings, that you're still using
                    version 1.4?.
                    Sebastian,
                    Yes, I was running this on a box that had 1.4 - I just tested it on a box
                    that has 2.3.5 and it runs perfect. Your changes also allow it to be run
                    on the boxes that still have 1.4
                    Thanks for all your help.

                    --
                    Randomly generated signature
                    "I have opinions of my own -- strong opinions --but I don't always agree with them."-G.W.Bush

                    Comment

                    • Gabriel Genellina

                      #11
                      Re: getting a process's PID

                      At Wednesday 27/12/2006 17:33, eldorado wrote:
                      >Yes, I was running this on a box that had 1.4 - I just tested it on a box
                      >that has 2.3.5 and it runs perfect. Your changes also allow it to be run
                      >on the boxes that still have 1.4
                      Ouch! 1.4 is really really ancient! Even the most conservative
                      libraries -like PIL- require at least 1.5


                      --
                      Gabriel Genellina
                      Softlab SRL






                      _______________ _______________ _______________ _____
                      Preguntá. Respondé. Descubrí.
                      Todo lo que querías saber, y lo que ni imaginabas,
                      está en Yahoo! Respuestas (Beta).
                      ¡Probalo ya!


                      Comment

                      • Tom Plunket

                        #12
                        Re: getting a process's PID

                        eldorado wrote:
                        >g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
                        >h = g.readlines()
                        >g.close()
                        >h
                        ['87334\012']
                        >h = h[:-1]
                        >h
                        []
                        >>
                        I understand you're probably set, but instead of using readlines() you
                        could also do this:

                        g = os....
                        h = g.read().split( '\n')

                        and then your 'h' list would not have newlines.

                        -tom!

                        --

                        Comment

                        Working...