Skip to content

Instantly share code, notes, and snippets.

@alister
Created November 22, 2011 17:04
Show Gist options
  • Save alister/1386212 to your computer and use it in GitHub Desktop.
Save alister/1386212 to your computer and use it in GitHub Desktop.
keep running cli-beanstalk-worker.php
#!/bin/bash
# runBeanstalkd-worker.sh
# a shell script that keeps looping until an exit code is given
# if it does an exit(0), restart after a second - or if it's a declared error
# if we've restarted in a planned fashion, we don't bother with any pause
# and for one particular code, exit the script entirely.
# The numbers 97, 98, 99 must match what is returned from the PHP script
nice php -q -f ./cli-beanstalk-worker.php -- $@
ERR=$?
## Possibilities
# 97 - planned pause/restart
# 98 - planned restart
# 99 - planned stop, exit.
# 0 - unplanned restart (as returned by "exit;")
# - Anything else is also unplanned paused/restart
if [ $ERR -eq 97 ]
then
# a planned pause, then restart
echo "97: PLANNED_PAUSE - wait 1";
sleep 1;
exec $0 $@;
fi
if [ $ERR -eq 98 ]
then
# a planned restart - instantly
echo "98: PLANNED_RESTART";
exec $0 $@;
fi
if [ $ERR -eq 99 ]
then
# planned complete exit
echo "99: PLANNED_SHUTDOWN";
exit 0;
fi
# unplanned exit, pause, and restart
echo "unplanned restart: err:" $ERR;
echo "sleeping for 1 sec"
sleep 1
exec $0 $@
@tapit69
Copy link

tapit69 commented Jan 1, 2015

One of the most useful scripts ever.. Could not work without it. Thanks very much!

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