Skip to content

Instantly share code, notes, and snippets.

@raymondbutcher
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raymondbutcher/e27316a6418c361aa565 to your computer and use it in GitHub Desktop.
Save raymondbutcher/e27316a6418c361aa565 to your computer and use it in GitHub Desktop.
A shell script that waits until a command returns successfully.
#!/usr/bin/env bash
#
# Usage: waitfor <command>
# Example: waitfor sudo service httpd status
#
# Waits until a command returns true (exit code: 0)
#
# Save or link this file as "waitwhile" to reverse the check,
# and wait while the command returns true.
#
script=`basename $0`
if [ -z $1 ]
then
echo "Usage: $script <command>"
echo "Example: $script sudo service httpd status"
exit 1
fi
if [ $script == "waitwhile" ]
then
while status=`$*`
do
echo $status
sleep 1
done
else
until status=`$*`
do
echo $status
sleep 1
done
fi
echo $status
@raymondbutcher
Copy link
Author

I'm using this for a release script, to block until things are ready.

Wait until puppet has finished running:

waitwhile ps -C puppet

Wait until apache is running:

waitfor sudo service httpd status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment