Skip to content

Instantly share code, notes, and snippets.

@hanleybrand
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanleybrand/a949c2f15fe200d9d709 to your computer and use it in GitHub Desktop.
Save hanleybrand/a949c2f15fe200d9d709 to your computer and use it in GitHub Desktop.
mdid_init_d
#!/bin/bash
### this is a script for a user (with sudo privs if launching mysql) to run.
### it's a script I use for developing on a mac, so it will need tweaking to run
### on linux systems (i.e. it will need all of the init.d, service stuff addded)
###
### it requires an argument - start|stop|restart|procs|pids
### start|stop|restart are as expected,
### procs invokes pgrep -fl for solr, rabbitmq, mysql and "python manage.py runworkers"
### pids invokes pgrep -f for solr, rabbitmq, mysql and "python manage.py runworkers"
USER_HOME="$HOME"
MDID_ROOT="$USER_HOME/Dev/hacking-mdid3"
ROOI_ROOT="$MDID_ROOT/rooibos"
DATA_ROOT="$USER_HOME/Dev/mdid-data/hacking-mdid3"
# path to your virtual env - if you aren't using a venv set these to empty strings,
VENV_ROOT="$USER_HOME/.virtualenvs/hm3"
VENV_START="$VENV_ROOT/bin/activate"
SOLR_DIR="$MDID_ROOT/solr"
EPMD="/usr/local/Cellar/rabbitmq/3.3.0/erts-5.10.3/bin/epmd"
# set MYSQL_START to false if you leave it running all the time
# note - if set to true you will be asked for a (sudo) password when the script runs
MYSQL_START=true
MYSQL_SVC="/usr/local/mysql/support-files/mysql.server"
# java -DSTOP.PORT=8079 -DSTOP.KEY=stopkey --stop
JAVA_OPTIONS="-Dsolr.solr.home=$SOLR_DIR/solr -server -DSTOP.PORT=8079 -DSTOP.KEY=stopkey -Xmx1024M -Xms1024M -jar $SOLR_DIR/start.jar"
JAVA="/usr/bin/java"
LOG_FILE="$DATA_ROOT/logs/solr.log"
echo "Manage MDID3 at $MDID_ROOT"
if [ -z "$1" ]
then
echo "You must include {start|stop|restart|procs} as an argument (e.g. './start_mdid3 start') "
exit 1
fi
######################### START IT UP #############################
case $1 in
start)
## mysql
if [ "$MYSQL_START" = true ]
then
echo "Starting mysql-server"
sudo $MYSQL_SVC start
fi
if [ "$MYSQL_START" = false ]
then
echo "Not starting mysql-server - if there are DB connection issues check to make sure mysql is running"
fi
## solr
echo "Starting Solr from"
cd $SOLR_DIR
pwd
nohup $JAVA $JAVA_OPTIONS 2> $LOG_FILE 2>&1 &
echo "ok - remember it may take a minute or two before Solr responds on requests"
# rabbitmq
echo "Starting rabbitmq-server from"
pwd
rabbitmq-server -detached
echo "rabbitmq-server ok"
# mdid-workers may need a few seconds
sleep 3
echo "Starting mdid-workers"
cd $ROOI_ROOT
source $VENV_START
python manage.py runworkers &
echo "mdid-workers ok"
while true; do
read -p "execute python manage.py runserver? (y/n)" yn
case $yn in
[Yy]* ) cd $ROOI_ROOT;python manage.py runserver; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
;;
######################### STOP STOP STOP #############################
stop)
echo "Stopping Solr"
cd $SOLR_DIR
$JAVA $JAVA_OPTIONS --stop
echo "ok"
echo "Stopping rabbitmq-server"
rabbitmqctl stop
echo "Killing epmd"
$EPMD -kill
echo "ok"
if [ "$MYSQL_START" = true ]
then
echo "Stopping mysql-server"
sudo /usr/local/mysql/support-files/mysql.server stop
echo "ok"
fi
;;
restart)
$0 stop
sleep 3
$0 start
;;
procs)
echo "Currently running processes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
# pgrep -f solr | { read message; echo "SOLR pid: $message"; }
echo -e "\n########################## solr"
pgrep -fl solr
echo -e "\n########################## rabbitmq-server"
pgrep -fl rabbitmq
echo -e "\n########################## mysql"
pgrep -fl mysql
echo -e "\n########################## mdid3 workers"
pgrep -fl "python manage.py runworkers"
echo -e "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
;;
pids)
echo "MDID 3 related process ids:"
echo -e "\n## solr"
pgrep -f solr
echo -e "\n## rabbitmq-server"
pgrep -f rabbitmq
echo -e "\n## mysql"
pgrep -f mysql
echo -e "\n## mdid3 workers"
pgrep -f "python manage.py runworkers"
echo -e "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
;;
*)
echo "Usage: $0 {start|stop|restart|procs|pids}" >&2exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment