30

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!

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
JohnP
  • 309
  • 1
  • 3
  • 3
  • 3
    Check out http://stackoverflow.com/questions/356100/how-to-wait-in-bash-for-several-subprocesses-to-finish-and-return-exit-code-0-w – atk Mar 05 '11 at 21:55
  • 3
    Please see [Process Management](http://mywiki.wooledge.org/ProcessManagement). 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. – Dennis Williamson Mar 05 '11 at 22:01
  • What usage of /dev/null seems to require root? Can't think of any. – ndemou Oct 27 '15 at 11:05

5 Answers5

39

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.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
26

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).

sligocki
  • 6,246
  • 5
  • 38
  • 47
  • 1
    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. – Peter Eisentraut Aug 15 '19 at 05:30
  • 1
    `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` – Dave Jan 26 '22 at 16:13
  • 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. – sligocki Jan 26 '22 at 20:23
10

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

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 2
    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. – Zouppen Apr 19 '12 at 13:06
  • In my tests, I see that directory hang around for a short time after the process has died – Bryan Sep 18 '14 at 10:27
8

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

returns 0 if it exists, 1 otherwise

Matt Kneiser
  • 1,982
  • 18
  • 23
  • 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 Jul 30 '15 at 04:01
3

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.

qdiesel
  • 401
  • 3
  • 9
  • 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. – ghoti Jul 30 '15 at 04:03