Skip to content

Instantly share code, notes, and snippets.

@rab
Created June 16, 2009 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rab/130967 to your computer and use it in GitHub Desktop.
Save rab/130967 to your computer and use it in GitHub Desktop.
Bash functions to start mongrel_rails and webrick applications
cons () { if [ -e Gemfile ]; then
be='bundle exec'
else
be=""
fi
if [ -x "script/console" ]; then
${be} ruby script/console ${1:-${RAILS_ENV:-development}}
else
${be} rails console ${1:-${RAILS_ENV:-development}}
fi
unset be
}
wr () { ruby script/server webrick ${1:+--port $1} ; }
mr () { if [[ $1 == -o* ]]; then
do_open='true'
page=${1#-o}
shift
fi
railsenv=${1:-${RAILS_ENV:-development}}
port=${2:-3000}
if [ -e log/mongrel.pid ]; then
echo do not wait for 'mongrel_rails restart' >/dev/null
mrs $railsenv $port
sleep 2
fi
if [ -e Gemfile ]; then
be='bundle exec'
else
be=""
fi
if echo -e "\035quit" | telnet 127.0.0.1 $port >/dev/null 2>&1; then
echo "${port} appears to be in use. What has ${port} open..."
stubbornPID=$(sudo -p "sudo wants %u's password to become %U on %h: " lsof -n -P -t -i tcp:${port})
if [ -n "${stubbornPID}" ]; then
echo Try to kill stubborn process with PID ${stubbornPID}
kill -s INT ${stubbornPID}
fi
else
echo "${port} is available"
fi
echo "== Starting Mongrel as daemon${1:+ for $1}${2:+ on port $2}" |
tee -a log/${railsenv}.log
${be} mongrel_rails start --daemonize ${1:+-e $1} ${2:+-p $2} ;
i=1
while [ $i -le 20 -a ! -e log/mongrel.pid ]; do
echo -n '.'; sleep 1
i=$(( i + 1 ))
done
if [ -e log/mongrel.pid ]; then echo "done"; ps -p $(<log/mongrel.pid)
else tail -20 log/mongrel.log; echo -e "\n*** Did not start! Do you see the problem? ^"; fi
unset railsenv
if [ "${do_open}" == 'true' ]; then
unset do_open
if [[ $page == /* ]]; then
page=http://localhost:${port}${page}
fi
open ${page:-http://localhost:${port}/}
fi
unset port
unset be
}
mrs () { if [ -e Gemfile ]; then
be='bundle exec'
else
be=""
fi
${be} mongrel_rails stop --force --wait 3 ; rm -f log/mongrel.pid ;
while [ -e log/mongrel.pid ] && ps -p $(<log/mongrel.pid); do echo -n '.'; sleep 1; done; echo "done"
echo "== Stopped at $(date)" >> log/${1:-${RAILS_ENV:-development}}.log
unset be
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment