|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Runs a Spark command as a daemon. |
| 4 | +# |
| 5 | +# Environment Variables |
| 6 | +# |
| 7 | +# SPARK_CONF_DIR Alternate conf dir. Default is ${SPARK_PREFIX}/conf. |
| 8 | +# SPARK_LOG_DIR Where log files are stored. PWD by default. |
| 9 | +# SPARK_MASTER host:path where spark code should be rsync'd from |
| 10 | +# SPARK_PID_DIR The pid files are stored. /tmp by default. |
| 11 | +# SPARK_IDENT_STRING A string representing this instance of spark. $USER by default |
| 12 | +# SPARK_NICENESS The scheduling priority for daemons. Defaults to 0. |
| 13 | +## |
| 14 | + |
| 15 | +usage="Usage: spark-daemon.sh [--config <conf-dir>] [--hosts hostlistfile] (start|stop) <spark-command> <args...>" |
| 16 | + |
| 17 | +# if no args specified, show usage |
| 18 | +if [ $# -le 1 ]; then |
| 19 | + echo $usage |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +bin=`dirname "$0"` |
| 24 | +bin=`cd "$bin"; pwd` |
| 25 | + |
| 26 | +. "$bin/spark-config.sh" |
| 27 | + |
| 28 | +# get arguments |
| 29 | +startStop=$1 |
| 30 | +shift |
| 31 | +command=$1 |
| 32 | +shift |
| 33 | + |
| 34 | +spark_rotate_log () |
| 35 | +{ |
| 36 | + log=$1; |
| 37 | + num=5; |
| 38 | + if [ -n "$2" ]; then |
| 39 | + num=$2 |
| 40 | + fi |
| 41 | + if [ -f "$log" ]; then # rotate logs |
| 42 | + while [ $num -gt 1 ]; do |
| 43 | + prev=`expr $num - 1` |
| 44 | + [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num" |
| 45 | + num=$prev |
| 46 | + done |
| 47 | + mv "$log" "$log.$num"; |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +if [ -f "${SPARK_CONF_DIR}/spark-env.sh" ]; then |
| 52 | + . "${SPARK_CONF_DIR}/spark-env.sh" |
| 53 | +fi |
| 54 | + |
| 55 | +if [ "$SPARK_IDENT_STRING" = "" ]; then |
| 56 | + export SPARK_IDENT_STRING="$USER" |
| 57 | +fi |
| 58 | + |
| 59 | +# get log directory |
| 60 | +if [ "$SPARK_LOG_DIR" = "" ]; then |
| 61 | + export SPARK_LOG_DIR="$SPARK_HOME/logs" |
| 62 | +fi |
| 63 | +mkdir -p "$SPARK_LOG_DIR" |
| 64 | +touch $SPARK_LOG_DIR/.spark_test > /dev/null 2>&1 |
| 65 | +TEST_LOG_DIR=$? |
| 66 | +if [ "${TEST_LOG_DIR}" = "0" ]; then |
| 67 | + rm -f $SPARK_LOG_DIR/.spark_test |
| 68 | +else |
| 69 | + chown $SPARK_IDENT_STRING $SPARK_LOG_DIR |
| 70 | +fi |
| 71 | + |
| 72 | +if [ "$SPARK_PID_DIR" = "" ]; then |
| 73 | + SPARK_PID_DIR=/tmp |
| 74 | +fi |
| 75 | + |
| 76 | +# some variables |
| 77 | +export SPARK_LOGFILE=spark-$SPARK_IDENT_STRING-$command-$HOSTNAME.log |
| 78 | +export SPARK_ROOT_LOGGER="INFO,DRFA" |
| 79 | +log=$SPARK_LOG_DIR/spark-$SPARK_IDENT_STRING-$command-$HOSTNAME.out |
| 80 | +pid=$SPARK_PID_DIR/spark-$SPARK_IDENT_STRING-$command.pid |
| 81 | + |
| 82 | +# Set default scheduling priority |
| 83 | +if [ "$SPARK_NICENESS" = "" ]; then |
| 84 | + export SPARK_NICENESS=0 |
| 85 | +fi |
| 86 | + |
| 87 | + |
| 88 | +case $startStop in |
| 89 | + |
| 90 | + (start) |
| 91 | + |
| 92 | + mkdir -p "$SPARK_PID_DIR" |
| 93 | + |
| 94 | + if [ -f $pid ]; then |
| 95 | + if kill -0 `cat $pid` > /dev/null 2>&1; then |
| 96 | + echo $command running as process `cat $pid`. Stop it first. |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + fi |
| 100 | + |
| 101 | + if [ "$SPARK_MASTER" != "" ]; then |
| 102 | + echo rsync from $SPARK_MASTER |
| 103 | + rsync -a -e ssh --delete --exclude=.svn --exclude='logs/*' --exclude='contrib/hod/logs/*' $SPARK_MASTER/ "$SPARK_HOME" |
| 104 | + fi |
| 105 | + |
| 106 | + spark_rotate_log $log |
| 107 | + echo starting $command, logging to $log |
| 108 | + cd "$SPARK_PREFIX" |
| 109 | + nohup nice -n $SPARK_NICENESS "$SPARK_PREFIX"/run $command "$@" > "$log" 2>&1 < /dev/null & |
| 110 | + echo $! > $pid |
| 111 | + sleep 1; head "$log" |
| 112 | + ;; |
| 113 | + |
| 114 | + (stop) |
| 115 | + |
| 116 | + if [ -f $pid ]; then |
| 117 | + if kill -0 `cat $pid` > /dev/null 2>&1; then |
| 118 | + echo stopping $command |
| 119 | + kill `cat $pid` |
| 120 | + else |
| 121 | + echo no $command to stop |
| 122 | + fi |
| 123 | + else |
| 124 | + echo no $command to stop |
| 125 | + fi |
| 126 | + ;; |
| 127 | + |
| 128 | + (*) |
| 129 | + echo $usage |
| 130 | + exit 1 |
| 131 | + ;; |
| 132 | + |
| 133 | +esac |
| 134 | + |
| 135 | + |
0 commit comments