Skip to content

Instantly share code, notes, and snippets.

@key-amb
Created Aug 25, 2016
Embed
What would you like to do?
A wrapper script to control hubot by forever
#!/usr/bin/env bash
set -euo pipefail
envfile=".env.hubot"
if [[ ! -e $envfile ]]; then
echo "[error] $envfile not found!"
exit 1
fi
. $envfile
help() {
pod2text $0
exit 1
}
status() {
if forever list | grep 'No forever processes running' &>/dev/null; then
echo '[info] hubot is not running.'
return 1
else
echo '[ok] hubot is running.'
return 0
fi
}
start() {
forever start -w -c /bin/sh ./bin/hubot --adapter slack --require scripts/envs/$HUBOT_ENVIRONMENT
echo '[info] hubot started.'
}
stop() {
forever stop -c /bin/sh ./bin/hubot
echo '[info] hubot stopped.'
}
stop-and-confirm() {
local try=0
stop
while true; do
: $((try++))
status &>/dev/null || break
sleep 1
echo "[info] wait for hubot to stop ... $try"
if ((try >= 3)); then
echo "[error] Timeout."
fi
done
}
case "${1:-}" in
"status" )
forever list
status
;;
"start" )
if status &>/dev/null; then
echo '[warn] hubot is already running.'
exit 1
fi
start
;;
"stop" )
if ! status &>/dev/null; then
echo '[warn] hubot is not running.'
exit 1
fi
stop
;;
"restart" )
if ! status &>/dev/null; then
echo '[warn] hubot is not running.'
else
stop-and-confirm
fi
start
;;
* )
help
;;
esac
exit
: <<'__EOF__'
=encoding utf8
=head1 NAME
B<hubotctl> - Wrapper script to control hubot by forever
=head1 SYNOPSYS
# Control hubot
./bin/hubotctl status|start|stop|restart
# Help
./bin/hubotctl help|--help
=head1 DESCRIPTION
Blah blah blah.
=cut
__EOF__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment