/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!

11 Comments

  1. Răzvan Lucescu își alege cu atenție garderoba. | StirileMedia.ro · May 2, 2011 Reply

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

  2. Gilberto Ainge · May 1, 2011 Reply

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

  3. 1 Mai în cimitir. | StirileMedia.ro · May 1, 2011 Reply

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

  4. Coleman · April 29, 2011 Reply

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

  5. Donny Crapanzano · April 29, 2011 Reply

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

  6. Rod Wardell · April 29, 2011 Reply

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

Leave a Reply