Skip to content

Instantly share code, notes, and snippets.

@jeremyjbowers
Created August 22, 2012 22:25
Show Gist options
  • Save jeremyjbowers/3430073 to your computer and use it in GitHub Desktop.
Save jeremyjbowers/3430073 to your computer and use it in GitHub Desktop.
A sample Varnish Bash script.
#!/bin/bash
#PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/programs/varnish/sbin
HOST=`hostname | cut -d. -f1`
DAEMON='/programs/varnish/sbin/varnishd'
PROC="varnishd"
NAME="varnish"
# This list contains the values for project arg ($2) case statement
# The values should match vcl files in /data/conf/varnish/PROJECT.vcl
# Please keep it alphabetical
# Don't forget to update the switch below
PROJECTS="
wapo_projects
stage_data_politics
school_rankings
wapo_local
wapo_politics
wapo_generic
wapo_sports
wapo_national
wapo_data_politics
"
case "$2" in
"wapo_projects")
DAEMON_ARGS="-s malloc,500M -T 0.0.0.0:2050 -a 0.0.0.0:10050"
;;
"stage_data_politics")
DAEMON_ARGS="-s malloc,50M -T 0.0.0.0:2300 -a 0.0.0.0:10300"
;;
"school_rankings")
DAEMON_ARGS="-s malloc,250M -T 0.0.0.0:2250 -a 0.0.0.0:10250"
;;
"wapo_local")
DAEMON_ARGS="-s malloc,250M -T 0.0.0.0:2251 -a 0.0.0.0:10251"
;;
"wapo_politics")
DAEMON_ARGS="-s malloc,50M -T 0.0.0.0:2350 -a 0.0.0.0:10350"
;;
"wapo_generic")
DAEMON_ARGS="-s malloc,50M -T 0.0.0.0:2400 -a 0.0.0.0:10400"
;;
"wapo_national")
DAEMON_ARGS="-s malloc,250M -T 0.0.0.0:2500 -a 0.0.0.0:10500"
;;
"wapo_sports")
DAEMON_ARGS="-s malloc,250M -T 0.0.0.0:2450 -a 0.0.0.0:10450"
;;
"wapo_data_politics")
DAEMON_ARGS="-s malloc,250M -T 0.0.0.0:2300 -a 0.0.0.0:10300"
;;
*)
echo "You must provide a valid project name."
echo "Choices are: $PROJECTS"
exit 1
;;
esac
# use this if we can get access to local disk
VAR_ROOT=/programs/varnish/var/varnish
VAR_PATH=$VAR_ROOT/$2
PID_ROOT=$VAR_ROOT/run
PID_PATH="${PID_ROOT}/${2}.pid"
CONFIG_FILE="/data/conf/varnish/${2}.vcl"
DAEMON_ARGS="$DAEMON_ARGS -f $CONFIG_FILE -n $VAR_PATH -P $PID_PATH -p http_req_hdr_len=20000 -p http_resp_hdr_len=20000 -p thread_pool_min=200 -p thread_pool_max=4000 -p thread_pool_add_delay=2 -p thread_pools=8 -p cli_timeout=25 -p session_linger=50 -p lru_interval=20"
test -x $DAEMON || echo "$DAEMON is not executable"
test -x $DAEMON || exit 1
test -d $VAR_PATH || echo "The directory $VAR_PATH does not exist"
test -d $VAR_PATH || exit 1
test -d $PID_ROOT || echo "The directory $PID_ROOT does not exist"
test -d $PID_ROOT || exit 1
test -e $CONFIG_FILE || echo "The file $CONFIG_FILE does not exist"
test -e $CONFIG_FILE || exit 1
# set the process count
# figure out if there is a pid and if that pid is active
if test -e $PID_PATH
then
PID=`cat $PID_PATH`
if [ $PID ]
then
PID_COUNT=`ps $PID | wc -l`
PID_COUNT=`expr $PID_COUNT - 1`
else
PID_COUNT=0
fi
else
PID_COUNT=0
fi
set -e
case "$1" in
start)
if [ $PID_COUNT == 0 ]
then
echo $DAEMON $DAEMON_ARGS
$DAEMON $DAEMON_ARGS
else
echo "Pid file exists and pid ($PID) is active."
echo "Is $NAME already running?"
fi
;;
stop)
if [ $PID_COUNT == 0 ]
then
echo "No processes matching '$PROC' found."
else
kill -9 $PID && echo > $PID_PATH
fi
;;
restart)
if [ $PID_COUNT == 0 ]
then
echo "No processes matching '$PROC' found."
echo "Try start instead"
else
echo "Shutting down"
kill -9 $PID
echo "Restarting"
$DAEMON $DAEMON_ARGS
fi
;;
*)
echo "Usage: $NAME start|stop|restart"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment