/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
Be Sociable, Share!