Check if PID exists in Bash

55,957

Solution 1

kill -s 0 $pid will return success if $pid is running, failure otherwise, without actually sending a signal to the process, so you can use that in your if statement directly.

wait $pid will wait on that process, replacing your whole loop.

Solution 2

It seems like you want

wait $pid

which will return when $pid finishes.

Otherwise you can use

ps -p $pid

to check if the process is still alive (this is more effective than kill -0 $pid because it will work even if you don't own the pid).

Solution 3

You might look for the presence of /proc/YOUR_PID directory.

Solution 4

ps --pid $pid &>/dev/null

returns 0 if it exists, 1 otherwise

Solution 5

I always use the following tail -f /dev/null --pid $PID. It doesn't require explicit loop and isn't limited to your shell's children pids only.

Share:
55,957

Related videos on Youtube

JohnP
Author by

JohnP

Updated on July 09, 2022

Comments

  • JohnP
    JohnP almost 2 years

    I want to stall the execution of my BASH script until a process is closed (I have the PID stored in a variable). I'm thinking

    while [PID IS RUNNING]; do
    sleep 500
    done
    

    Most of the examples I have seen use /dev/null which seems to require root. Is there a way to do this without requiring root?

    Thank you very much in advance!

    • atk
      atk about 13 years
    • SourceSeeker
      SourceSeeker about 13 years
      Please see Process Management. Can you show an example of using /dev/null which requires root? I can't imagine how that would be the case or how it would be applicable to this case.
    • ndemou
      ndemou over 8 years
      What usage of /dev/null seems to require root? Can't think of any.
  • JohnP
    JohnP about 13 years
    Thanks. Here is my current code: while sleep 1; do kill -0 $PIDD || break; done.
  • Zouppen
    Zouppen about 12 years
    That's okay, if you are running the script in Linux only. To assure compatibility, it's better to use kill -s trick given above.
  • sligocki
    sligocki almost 12 years
    Note: kill -0 $pid only works for processes you own. ps -p $pid will work for any process.
  • Harish
    Harish over 11 years
    Note: wait $pid only works if you are the parent of $pid.
  • Bryan
    Bryan over 9 years
    In my tests, I see that directory hang around for a short time after the process has died
  • ghoti
    ghoti almost 9 years
    The --pid option for ps may work in your operating system, but it doesn't work in mine. Did the OP mention what OS he's running? If not, you should qualify answers you provide which are OS-specific and non-portable.
  • ghoti
    ghoti almost 9 years
    The tail command on FreeBSD and OSX does not include a --pid option. If the OP isn't asking about a particular operating system, you should qualify any answers you provide which are platform-specific.
  • Peter Eisentraut
    Peter Eisentraut over 4 years
    Note: wait only works if $pid is a child process of the shell. That is probably often the case in these scenarios, but not always.
  • Dave
    Dave over 2 years
    wait is not useful when scripting. If the goal is to quickly determine if a pid exists, then do something, wait would have be be bg which is inefficient. The second solution is better. Include removing the header and its usable: ps -p $pid | tail -n +2
  • sligocki
    sligocki over 2 years
    Dave: The question literally states: "I want to stall the execution of my BASH script until a process is closed" which is exactly what wait does. wait is perfectly usable in a script, I have used it many times! It simply depends what your use-case is.