Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi friends, can anyone tell me how do i get(using coding) a pid of the process that is running when i know only the name of the process.... please gimme ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    76

    get the PID


    Hi friends,
    can anyone tell me how do i get(using coding) a pid of the process that is running when i know only the name of the process.... please gimme a ready code to find the pid of the process when i know only the process name.. ?
    thanks..

  2. #2
    Just Joined!
    Join Date
    Apr 2007
    Posts
    11

    pid

    Run "ps -ef | grep process name".

    Quote Originally Posted by lovlin
    Hi friends,
    can anyone tell me how do i get(using coding) a pid of the process that is running when i know only the name of the process.... please gimme a ready code to find the pid of the process when i know only the process name.. ?
    thanks..

  3. #3
    Just Joined!
    Join Date
    Aug 2006
    Posts
    76
    thanks for reply Faizsir
    will this give the process ID when is give like..."ps -ef | grep pppd" ????...can i get some sort of C code for this ???

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

    Smile

    Lovlin... you have the answer in the title of this thread itself
    Use getpid() system call.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  5. #5
    Just Joined!
    Join Date
    Aug 2006
    Posts
    76
    thanks for reply Lakshmipathi,
    but when i am writing a C code....it gives the pid of the process that contains main right ??? so inside the main say if i am spawning sshd then how do i get the sshd's pid knowing that the name of the process is "sshd"

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

    Smile

    are using fork() to create a new process sshd...
    if so check man fork().
    please cut n paste some codes...
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  7. #7
    Just Joined!
    Join Date
    Sep 2006
    Posts
    22
    Hi lovlin,
    I got your issue. There is a shell command called "pgrep". To get the pid of process for which you know its name, tune it like below.

    Code:
     pgrep -x <process name>

  8. #8
    Just Joined!
    Join Date
    Sep 2006
    Posts
    22
    Hi lovlin,
    I got your issue. There is a shell command called "pgrep", which will throw the PID into STDOUT. To get the pid of process for which you know its name, tune it like below.
    Code:
     pgrep -x <process name>
    But as this is shell command, it can be used only with system() or with exec(). If someone else comes up with system call equivalent of pgrep, it would be great.

  9. #9
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    Hi,

    when you are looking for PID with in a process which is spawning then you can alway use the getpid() call. because the fork() will return the pid of the child to the parent and 0 to the child.

    Let me know your requirements.

    --REgards
    rajesh

  10. #10
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    an even simpler way would be to use the "pidof" command.

    Code:
    pidof firefox-bin
    will return imaginitively enough the process id of the command "firefox-bin"
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

Page 1 of 2 1 2 LastLast

Posting Permissions

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