/usr/local/bin/waitpid

A quick script to wait for a certain pid, then once that quits, execute a command.
You may ask, why not just do command1 && command2 ? Well, if command1 exits with a non-zero exit status value, command2 will not get executed. Hence, I’ve brewed a quick script for this purpose.

File: /usr/local/bin/waitpid

#!/bin/sh
# Find the pid of the required process either by using psaux | grep -i command
# or by pidof command
#set -x
if [ $# -ne 3 ]; then
	echo "Usage: waitpid [pid to wait for] [app name] [command]"
	echo "       [command] should be command to execute after pid is dead"
	exit 1
fi
pidr=$1
app=$2
while true; do
	pid=`pidof $app | grep -o $pidr`
	if [ "$pid" = "$pidr" ]; then
		# do nothing
		echo "pid does exist,"
		echo "waiting for 5 seconds before next check"
		sleep 5
		continue
	fi
	break
done
# if we've reached here it means that the PID is dead
echo "specified pid does NOT exist,"
echo "running command provided in 10 seconds"
echo ""
echo "Press Ctrl + c to stop"
sleep 10
$3

Comments

11 responses to “/usr/local/bin/waitpid”

  1. […] /usr/local/bin/waitpid | jude pereira's blog […]

  2. […] /usr/local/bin/waitpid | jude pereira's blog […]

  3. You know, I actually posted a comparable things I found on your website. Your is much better.

  4. […] /usr/local/bin/waitpid | jude pereira's blog […]

  5. Perfect contribution, I must say i count on posts by you.

  6. What a neat story here, great article I enjoyed it very much, really cool!!!!! Let me know about new posts, Donny Crapanzano

  7. This really is great website. We appreciate you sharing it around!