Search code examples
linuxshellpid

How to run a program and know its PID in Linux?


How to run a program and know its PID in Linux?

If I have several shells running each other, will they all have separate PIDs?


Solution

  • Greg's wiki to the rescue:

    • $! is the PID of the last backgrounded process.
    • kill -0 $PID checks whether $PID is still running. Only use this for processes started by the current process or its descendants, otherwise the PID could have been recycled.
    • wait waits for all children to exit before continuing.

    Actually, just read the link - It's all there (and more).

    $$ is the PID of the current shell.

    And yes, each shell will have its own PID (unless it's some homebrewed shell which doesn't fork to create a "new" shell).