Skip to content

Instantly share code, notes, and snippets.

@davidhooey
Last active May 24, 2016 21:48
Show Gist options
  • Save davidhooey/5454747 to your computer and use it in GitHub Desktop.
Save davidhooey/5454747 to your computer and use it in GitHub Desktop.
GemInABox init.d Script
#! /bin/bash
# GemInABox
# Maintainer: David Hooey
### BEGIN INIT INFO
# Provides: geminabox
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Gem in a Box
### END INIT INFO
APP_ROOT="/home/rubygems"
RACK_PID="$APP_ROOT/rack.pid"
RACK_UP="/usr/local/bin/rackup -E production -D -P $RACK_PID"
NAME="rack"
DESC="GemInABox service"
check_pid(){
if [ -f $RACK_PID ]; then
PID=`cat $RACK_PID`
STATUS=`ps aux | grep $PID | grep -v grep | wc -l`
else
STATUS=0
PID=0
fi
}
start() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
# Program is running, exit with error code 1.
echo "Error! $DESC $NAME is currently running!"
exit 1
else
if [ `whoami` = root ]; then
sudo -u rubygems -H bash -l -c "$RACK_UP"
echo "$DESC started"
fi
fi
}
stop() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
## Program is running, stop it.
kill -QUIT `cat $RACK_PID`
rm "$RACK_PID" >> /dev/null
echo "$DESC stopped"
else
## Program is not running, exit with error.
echo "Error! $DESC not started!"
exit 1
fi
}
status() {
cd $APP_ROOT
check_pid
if [ "$PID" -ne 0 -a "$STATUS" -ne 0 ]; then
echo "$DESC with PID $PID is running."
else
echo "$DESC is not running."
exit 1
fi
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: sudo service geminabox {start|stop|restart}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment