Find the answer to your Linux question:
Results 1 to 6 of 6
Say I want to run a command says top command, I need to get process id after invoking it. something like $pid= top $echo $pid #pid of just invoked command ...
  1. #1
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation [SOLVED] How to get process id - pid ?

    Say I want to run a command says top command,
    I need to get process id after invoking it. something like

    $pid= top
    $echo $pid #pid of just invoked command

    I don't want to use something like ps aux | grep top - since I have several top command running in parallel.

    Thanks
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  2. #2
    Just Joined!
    Join Date
    Jan 2005
    Posts
    17
    Quote Originally Posted by Lakshmipathi View Post
    Say I want to run a command says top command,
    I need to get process id after invoking it. something like

    $pid= top
    $echo $pid #pid of just invoked command

    I don't want to use something like ps aux | grep top - since I have several top command running in parallel.

    Thanks
    What do you hope to achieve?

  3. #3
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    I'm testing an application ...
    start application1 -> get pid -> do some testing -> kill process using pid
    start application2 -> get pid -> do some testing -> kill process using pid
    application1,application2 ... are all same process created by same user.
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,970
    On most distributions there is a command pidof which will return the process id's of named applications. If you want the pid of a background process when you start it, then you can use the shell variable $! which will return the pid of the last background process started in that shell. This is useful for shell scripts.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    Quote Originally Posted by Rubberman View Post
    you can use the shell variable $! which will return the pid of the last background process started in that shell. This is useful for shell scripts.
    Spot on !! The $! will suit my requirements ...thanks Rubberman
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,970
    Glad to be of service!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...