Created
July 12, 2011 22:01
-
-
Save rchampourlier/1079082 to your computer and use it in GitHub Desktop.
SystemV service startup script for a Ruby On Rails app with unicorn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# app Rails application served through an Unicorn instance | |
# | |
# Author Romain Champourlier @ softr.li | |
# | |
# chkconfig: - 87 13 | |
# | |
# description: This a web application developed in Ruby On Rails | |
# which is served through an Unicorn instance. | |
# processname: unicorn_rails | |
# config: /path/to/your/app/config/unicorn.rb | |
# pidfile: /path/to/your/app/tmp/pids/unicorn.pid | |
# | |
### BEGIN INIT INFO | |
# Provides: app | |
# Default-Start: 2 3 4 5 | |
# Default-Stop: 0 1 6 | |
# Short-Description: unicorn running app | |
# Description: Unicorn instance running the a web app. | |
### END INIT INFO | |
# source function library | |
. /etc/rc.d/init.d/functions | |
APP_NAME="your_app_name" | |
APP="/path/to/your/$APP_NAME" | |
RVM_GEMSET="ruby-1.9.2-p290@gemset" | |
PIDFILE="$APP/tmp/pids/unicorn.pid" | |
LOCKFILE=/var/lock/subsys/$APP_NAME | |
RETVAL=0 | |
USER=www | |
ENVIRONMENT=production | |
CD="cd $APP/" | |
RVM_USE="rvm use $RVM_GEMSET &>/dev/null" | |
SERVER_START="unicorn_rails -c config/unicorn.rb -E $ENVIRONMENT -D" | |
CMD="$CD; $RVM_USE; $SERVER_START" | |
start() { | |
echo -n $"Starting Unicorn for '$APP_NAME': " | |
# Looking at the pidfile to look if the server is already running. | |
should_start=1 | |
if [ -f $PIDFILE ]; then | |
checkpid `cat $PIDFILE` | |
running=$? | |
if [ $running -eq 0 ]; then | |
# There is a pidfile and a process for that pid. We assume the | |
# server is running and we pass. | |
echo_passed | |
else | |
# There is a pidfile but no process for that pid. We remove the | |
# pidfile and set the server to be started. | |
rm -f $PIDFILE | |
should_start=0 | |
fi | |
else | |
# No pidfile. Should start. | |
should_start=0 | |
fi | |
if [ $should_start -eq 0 ]; then | |
su - $USER -c "$CMD" | |
if [ -f $PIDFILE ]; then | |
# If there is a pidfile now, assuming success. | |
echo_success | |
RETVAL=0 | |
else | |
# If there is no pidfile, assuming failure. | |
echo_failure | |
RETVAL=1 | |
fi | |
fi | |
echo | |
[ $RETVAL -eq 0 ] && touch $LOCKFILE | |
return $RETVAL | |
} | |
stop () { | |
echo -n $"Stopping Unicorn for '$APP_NAME': " | |
killproc -p $PIDFILE | |
RETVAL=$? | |
echo | |
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE $PIDFILE | |
} | |
case "$1" in | |
start) | |
start | |
;; | |
stop) | |
stop | |
;; | |
restart) | |
stop | |
start | |
;; | |
status) | |
if [ -f $PIDFILE ]; then | |
checkpid `cat $PIDFILE` | |
running=$? | |
if [ $running -eq 0 ]; then | |
echo $"$APP_NAME is running..." | |
else | |
echo $"$APP_NAME is stopped" | |
fi | |
fi | |
;; | |
*) | |
echo $"Usage: $0 {start|stop|restart}" | |
RETVAL=2 | |
esac | |
exit $RETVAL |
Added a basic status implementation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improved the script to detect service state when starting/stopping according to the pidfile. Now displaying correctly OK or FAILURE according to the results of the request. Better structure too.
The script will start the unicorn server with a given user, and in a given RVM gemset.