Skip to content

Instantly share code, notes, and snippets.

@bgdnlp
Last active November 14, 2015 08:26
Show Gist options
  • Save bgdnlp/5a81ee71ff11c1ede26e to your computer and use it in GitHub Desktop.
Save bgdnlp/5a81ee71ff11c1ede26e to your computer and use it in GitHub Desktop.
Azkaban web server init.d script
#!/bin/bash
#
# azkaban-web Start Up Azkaban web server
#
# chkconfig: 2345 95 5
# description: Azkaban allows users to run queries on EMR (Hadoop) clusters \
# via a web interface
### BEGIN INIT INFO
# Provides: azkaban
# Required-Start: $network $local_fs $remote_fs $syslog
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop Azkaban Web Server
# Description: Azkaban allows users to run queries on EMR (Hadoop) clusters \
# via a web interface
### END INIT INFO
# TODO: would be nice if status messages would be right-aligned and colored
# Source function library.
. /etc/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
prog=azkaban-web
azkaban_dir="/opt/azkaban-web"
config_dir="{{ azkaban_dir }}/conf/"
logs_dir="/var/log/azkaban-web"
tmpdir="/tmp"
pid_file="/var/run/azkaban-web.pid"
start() {
[ "$EUID" != "0" ] && exit 4
[ "$NETWORKING" = "no" ] && exit 1
[ -d "$config_dir" ] || exit 5
#. /etc/sysconfig/azkaban-web
RETVAL=0
PATH=$azkaban_dir:$PATH # probably not necessary
# add all azkaban jar files to CLASSPATH
for file in $azkaban_dir/lib/*.jar;
do
CLASSPATH=$CLASSPATH:$file
done
for file in $azkaban_dir/extlib/*.jar;
do
CLASSPATH=$CLASSPATH:$file
done
for file in $azkaban_dir/plugins/*/*.jar;
do
CLASSPATH=$CLASSPATH:$file
done
if [ "$HADOOP_HOME" != "" ]; then
CLASSPATH=$CLASSPATH:$HADOOP_HOME/conf:$HADOOP_HOME/*
JAVA_LIB_PATH="-Djava.library.path=$HADOOP_HOME/lib/native/Linux-amd64-64"
else
logger -s "azkaban-web init.d: HADOOP_HOME is not set. Hadoop job types will not run properly."
fi
if [ "$HIVE_HOME" != "" ]; then
CLASSPATH=$CLASSPATH:$HIVE_HOME/conf:$HIVE_HOME/lib/*
fi
executorport=`cat $azkaban_dir/conf/azkaban.properties | grep executor.port | cut -d = -f 2`
serverpath=$azkaban_dir
if [ -z "$AZKABAN_OPTS" ]; then
AZKABAN_OPTS="-Xmx3G"
fi
AZKABAN_OPTS="$AZKABAN_OPTS -server -Dcom.sun.management.jmxremote -Djava.io.tmpdir=$tmpdir -Dexecutorport=$executorport -Dserverpath=$serverpath -Dlog4j.log.dir=$logs_dir"
echo -n "Starting azkaban-web: "
# check if there's already a process up. blissfully ignores pid file
ps auxww | grep "[a]zkaban\.webapp\.AzkabanWebServer" > /dev/null
if [ "$?" -eq 0 ]; then
# already started should be considered success
# but we avoid starting a second one
echo "[OK]"
return 0
fi
# start azkaban
java $AZKABAN_OPTS $JAVA_LIB_PATH -cp $CLASSPATH \
azkaban.webapp.AzkabanWebServer -conf $azkaban_dir/conf $@ &
sleep 2
running_pid=`ps auxww | grep "[a]zkaban\.webapp\.AzkabanWebServer" | head -1 | awk '{print $2}'`
if [ -z "$running_pid" ]; then
echo "[FAILED]"
return 7
fi
echo "$running_pid" > $pid_file
echo "[OK]"
}
stop() {
echo -n "Stopping azkaban-web: "
running_pid=`ps auxww | grep "[a]zkaban\.webapp\.AzkabanWebServer" | head -1 | awk '{print $2}'`
if [ -z "$running_pid" ]; then
# attempting to stop a process that isn't running should be
# considered succesful. remove $pid_file if exists
if [ -f "$pid_file" ]; then
rm $pid_file
fi
echo "[OK]"
return 0
fi
if [ ! -f "$pid_file" ]; then
# we know that azkaban is running, otherwise it would have been
# dealt with in the if above
echo "[FAILED - no PID file, but azkaban running ($running_pid)]"
return 13
fi
pid=`cat $pid_file`
if [ "$pid" -gt 1 -a "$pid" == "$running_pid" ]; then
kill $pid
sleep 1
fi
ps auxww | grep "[a]zkaban\.webapp\.AzkabanWebServer" > /dev/null
if [ "$?" -eq 0 ]; then
echo "[FAILED]"
return 13
fi
rm $pid_file
echo "[OK]"
}
status() {
# check if there's any azkaban process running or a pid file present
# if neither, then say it's stopped and return proper code
running_pid=`ps auxww | grep "[a]zkaban\.webapp\.AzkabanWebServer" | head -1 | awk '{print $2}'`
if [ -z "$running_pid" -a ! -f "$pid_file" ]; then
echo "azkaban-web is stopped"
return 3
fi
# something is present. check if all good
if [ ! -f $pid_file ]; then
echo "azkaban-web (pid $running_pid) is running, but $pid_file doesn't exist"
return 0
fi
pid=`cat $pid_file`
if [ "$pid" -gt 0 -a "$pid" == "$running_pid" ]; then
echo "azkaban-web (pid $running_pid) is running..."
return 0
fi
# we got to this point, so something isn't quite right
if [ -z "$running_pid" ]; then
# pid_file exists, but azkaban isn't running
echo "$pid_file exists but there is no azkaban process running"
return 1
fi
if [ -z "$pid" ]; then
echo "$pid_file contains no PID"
return 4
fi
echo "PID in $pid_file ($pid) is different than running process' PID ($running_pid)"
return 4
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
reload)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
@bgdnlp
Copy link
Author

bgdnlp commented Nov 14, 2015

init.d script for Azkaban Web Server. To be used with Linux distributions that didn't move to systemd, like Amazon Linux. Written around the LSB spec (https://refspecs.linuxbase.org/LSB_3.1.0/LSB-generic/LSB-generic/iniscrptact.html), also uses some additional exit codes.

The start() function is pretty much Azkaban's start script with a few changes, like also sending the HADOOP_HOME message to syslog and the maximum java heap to 3GB instead of 4.

stop() will not kill azkaban if the pid_file doesn't exist, even if it finds it running in memory.

Assumes only one Azkaban process running on the system, check how running_pid variable is set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment