how can I find out the process ids with a process name

Discussion in 'Python' started by herman, Sep 2, 2007.

  1. herman

    herman Guest

    Hi,

    I would like to find out all the process id with the process name
    'emacs'.

    In the shell, i can do this:

    $ ps -ef |grep emacs
    root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
    root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
    root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
    stressTestVideo.py
    root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
    gtk
    root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs


    and I can see the process id is 20731, 25649, etc, etc.

    But now I would like to do the programmically in my python script.
    I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
    grep emacs', but how
    can I pipe the output of my 'ps -ef | grep emacs' to my python script
    and then run a regression expression with it to get the process Ids?

    Thank you.
    herman, Sep 2, 2007
    #1
    1. Advertising

  2. herman

    Furkan KURU Guest

    the easiest but slowest way:

    you can send output to a file

    ps -ef |grep emacs > output_file

    and then read the file content

    (I believe there is a much better way)




    On 9/2/07, herman <> wrote:
    > Hi,
    >
    > I would like to find out all the process id with the process name
    > 'emacs'.
    >
    > In the shell, i can do this:
    >
    > $ ps -ef |grep emacs
    > root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
    > root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
    > root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
    > stressTestVideo.py
    > root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
    > gtk
    > root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs
    >
    >
    > and I can see the process id is 20731, 25649, etc, etc.
    >
    > But now I would like to do the programmically in my python script.
    > I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
    > grep emacs', but how
    > can I pipe the output of my 'ps -ef | grep emacs' to my python script
    > and then run a regression expression with it to get the process Ids?
    >
    > Thank you.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    Furkan Kuru
    Furkan KURU, Sep 2, 2007
    #2
    1. Advertising

  3. On Sep 2, 2007, at 12:26 PM, herman wrote:

    > I would like to find out all the process id with the process name
    > 'emacs'.
    >
    > In the shell, i can do this:
    >
    > $ ps -ef |grep emacs
    > root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
    > root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
    > root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
    > stressTestVideo.py
    > root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
    > gtk
    > root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs
    >
    >
    > and I can see the process id is 20731, 25649, etc, etc.
    >
    > But now I would like to do the programmically in my python script.
    > I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
    > grep emacs', but how
    > can I pipe the output of my 'ps -ef | grep emacs' to my python script
    > and then run a regression expression with it to get the process Ids?


    Are you targeting Linux? If so, have a look at the /proc system.
    Each process has a directory, and the 'status' file in each process'
    directory tells many things, including process name (the line that
    ends with the process name, begins with 'Name').

    Here's a quick bashy way to get pid + process names:

    cd /proc
    for i in ls [0-9]*/status
    do
    echo $i `grep '^Name' $i | cut -f2` | sed 's/\/status//g'
    done


    hth,
    Michael
    ---
    "If we had asked people what they wanted they would have said 'a
    faster horse'." --Henry Ford
    Michael Bentley, Sep 2, 2007
    #3
  4. >
    > cd /proc
    > for i in ls [0-9]*/status
    > do
    > echo $i `grep '^Name' $i | cut -f2` | sed 's/\/status//g'
    > done
    >



    Um...

    cd /proc
    for i in `ls [0-9]*/status`
    do
    echo $i `grep '^Name' $i | cut -f2` | sed 's/\/status//g'
    done


    ---
    Let the wookie win.
    Michael Bentley, Sep 2, 2007
    #4
  5. On Sun, 02 Sep 2007 19:26:27 +0000, herman wrote:

    > But now I would like to do the programmically in my python script. I
    > know I can use ' os.system(cmd)' to execute the command 'ps -ef | grep
    > emacs', but how
    > can I pipe the output of my 'ps -ef | grep emacs' to my python script
    > and then run a regression expression with it to get the process Ids?


    Use popen.

    >>> f = os.popen('ps ax | grep -i PYTHON')
    >>> print f.read()

    1952 ? Ssl 0:01 /usr/bin/python -E /usr/sbin/setroubleshootd
    2117 ? S 0:00 python ./hpssd.py
    2376 ? SN 3:19 /usr/bin/python /usr/sbin/yum-updatesd
    18087 pts/4 S+ 0:00 python
    18115 pts/4 S+ 0:00 sh -c ps ax | grep -i PYTHON
    18117 pts/4 R+ 0:00 grep -i python


    There is also a module popen2 which does similar but more advanced things.



    --
    Steven.
    Steven D'Aprano, Sep 2, 2007
    #5
  6. On Sep 2, 12:26 pm, herman <> wrote:
    > Hi,
    >
    > I would like to find out all the process id with the process name
    > 'emacs'.
    >
    > In the shell, i can do this:
    >
    > $ ps -ef |grep emacs
    > root 20731 8690 0 12:37 pts/2 00:00:09 emacs-snapshot-gtk
    > root 25649 25357 0 13:55 pts/9 00:00:05 emacs-snapshot-gtk rtp.c
    > root 26319 23926 0 14:06 pts/7 00:00:04 emacs-snapshot-gtk
    > stressTestVideo.py
    > root 26985 1 0 14:15 ? 00:00:01 /usr/bin/emacs-snapshot-
    > gtk
    > root 27472 21066 0 14:23 pts/5 00:00:00 grep emacs
    >
    > and I can see the process id is 20731, 25649, etc, etc.
    >
    > But now I would like to do the programmically in my python script.
    > I know I can use ' os.system(cmd)' to execute the command 'ps -ef |
    > grep emacs', but how
    > can I pipe the output of my 'ps -ef | grep emacs' to my python script
    > and then run a regression expression with it to get the process Ids?
    >


    Try commands module; it's simple to just get the output. subprocess
    module is a newer way to doing things. But commands.getoutput() is lot
    simpler for simple shell like tasks.

    >>> import commands
    >>> commands.getoutput("ps -ef | grep emacs | awk '{print $2}'")

    '21739\n15937\n15287\n5097\n14797\n31777\n8779\n2973\n5413\n13024\n13026'
    >>>


    Your script can then use the output as its input.

    Karthik

    > Thank you.
    Karthik Gurusamy, Sep 5, 2007
    #6
    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. Lenny
    Replies:
    2
    Views:
    388
    Lenny
    Jul 14, 2005
  2. Replies:
    2
    Views:
    695
    Beat Bolli
    Jun 21, 2005
  3. TJ
    Replies:
    1
    Views:
    123
    Mike Moore [MSFT]
    Oct 3, 2003
  4. Robert Mohr

    process ids

    Robert Mohr, Jan 15, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    75
    Robert Mohr
    Jan 16, 2004
  5. Replies:
    6
    Views:
    155
Loading...

Share This Page