Skip to content

Instantly share code, notes, and snippets.

@me-vlad
Created March 18, 2012 03:41
Show Gist options
  • Save me-vlad/2068604 to your computer and use it in GitHub Desktop.
Save me-vlad/2068604 to your computer and use it in GitHub Desktop.
init script for multiple thin instances with rbenv shared install
#!/bin/sh
# thin This shell script takes care of starting and stopping
# thin daemon from rbenv shared install
#
# chkconfig: 2345 85 15
# description: thin is an Ruby web server
# config: /etc/sysconfig/thin
### BEGIN INIT INFO
# Provides: thin
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# X-Start-Before: nginx
# X-Stop-After: nginx
# Short-Description: thin initscript
# Description: This shell script takes care of starting and
# stopping thin daemon from rbenv shared install
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
RBENV_DIR="/opt/rbenv"
THIN_BIN="$RBENV_DIR/shims/thin"
CONFIG_DIR="/etc/thin"
CONFIG_FILE_EXT=".yml"
[ -f /etc/sysconfig/thin ] && . /etc/sysconfig/thin
if [ ! -x "$THIN_BIN" ]; then
echo "error: thin executable does not exist or not executable: $THIN_BIN" && exit 1
fi
if [ -d "$RBENV_DIR" ]; then
export RBENV_ROOT="$RBENV_DIR"
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
else
echo "error: rbenv directory does not exist: $RBENV_DIR" && exit 1
fi
thin_ctl() {
local command=$1; shift
[ ! -d $CONFIG_DIR ] && echo "error: directory does not exist: $CONFIG_DIR" && exit 1
if [ $# -eq 0 ]; then
CONFIG_FILES=`cd ${CONFIG_DIR} && ls *${CONFIG_FILE_EXT}`
else
for app_name in "$@"; do
CONFIG_FILES="$CONFIG_FILES ${app_name}${CONFIG_FILE_EXT}"
done
fi
for config_file in ${CONFIG_FILES[@]}; do
echo "thin $command ${config_file//$CONFIG_FILE_EXT}"
[ ! -e "${CONFIG_DIR}/${config_file}" ] && echo "error: file does not exist: ${CONFIG_DIR}/${config_file}" && exit 1
case $command in
start|stop|restart)
chdir=`egrep "^chdir:" "${CONFIG_DIR}/${config_file}" | awk '{ print $2 }'`
[ ! -d "$chdir" ] && echo "error: chdir directory does not exist: $chdir" && exit 1
cd "$chdir"
"$THIN_BIN" -C "${CONFIG_DIR}/${config_file}" $command
;;
*)
echo "Unknown command ${command}"
;;
esac
done
return 0
}
enable_application() {
old="$1$CONFIG_FILE_EXT.disabled"
new="$1$CONFIG_FILE_EXT"
[ ! -e "$CONFIG_DIR/$old" ] && echo "error: file does not exist: $CONFIG_DIR/$old" && exit 1
mv "$CONFIG_DIR/$old" "$CONFIG_DIR/$new"
}
disable_application() {
old="$1$CONFIG_FILE_EXT"
new="$1$CONFIG_FILE_EXT.disabled"
[ ! -e "$CONFIG_DIR/$old" ] && echo "error: file does not exist: $CONFIG_DIR/$old" && exit 1
mv "$CONFIG_DIR/$old" "$CONFIG_DIR/$new"
}
config() {
echo "Creating config directory: $CONFIG_DIR"
[ ! -d "$CONFIG_DIR" ] && mkdir "$CONFIG_DIR"
echo "Creating config example: $CONFIG_DIR/example${CONFIG_FILE_EXT}.disabled"
cd /var/www/html/sl/redmine
"$THIN_BIN" config -C "${CONFIG_DIR}/example${CONFIG_FILE_EXT}.disabled" \
-u nobody -g nobody --tag example \
-s 4 --max-conns 1024 --max-persistent-conns 512 \
-S /tmp/example.sock -c /var/www/html/example \
-l /var/log/thin/example.log -P /var/run/example.pid \
-A autodetect -e production
}
if [ $# -gt 2 ]; then
exit 2
fi
case "$1" in
start|stop|restart)
thin_ctl "$@"
;;
enable|disable)
[ -z "$2" ] && echo "error: missing app_name" && exit 1
$1_application "$2"
;;
config)
config
;;
*)
echo "Usage: $0 {start|stop|restart|enable|disable|config} [app_name]"
exit 2
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment