Skip to content

Instantly share code, notes, and snippets.

@rumkin
Created August 13, 2011 18:01
Show Gist options
  • Save rumkin/1144098 to your computer and use it in GitHub Desktop.
Save rumkin/1144098 to your computer and use it in GitHub Desktop.
Respawn node application on error. Using for test perpouses
# Default respawn delay value
delay=5
# Default environment value
env="development"
#Show help
if [[ "$@" = "--help" || "$@" = "-h" ]]; then
cat <<EOF
Usage: bash spawn.sh [FILE...] [OPTIONS...]
Respawn application on failure
-d delay in seconds
-e environment: production or development
EOF
exit;
fi
#Getting filename
app=$1
args="${@:2}"
# Parse options
while getopts ":e:d:" optname $args; do
value="$OPTARG"
case "$optname" in
"h")
echo "Usage is..."
exit;
;;
"d")
delay="$value"
;;
"e")
env="$value"
;;
?) printf "Unknown option\n"
;;
*) printf "Error while parsing options\n"
;;
esac
done
if [ "$delay" -lt 1 ]; then
delay=1
fi
export NODE_ENV="$env"
while [ true ]; do
#clear screen
reset
#run node
node $app $env &
# if node succesfully exit stop respawning
if wait "$!"; then
printf "\n\nApplication exits\n"
exit 0;
else
printf "\n\nrespawn in"
count="$delay"
# Countdown
while [ $count -gt 0 ]; do
printf " "$count
let count=count-1
sleep 1
done
fi
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment