1 | #!/usr/bin/env bash |
---|
2 | |
---|
3 | # Licensed to the Apache Software Foundation (ASF) under one or more |
---|
4 | # contributor license agreements. See the NOTICE file distributed with |
---|
5 | # this work for additional information regarding copyright ownership. |
---|
6 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
7 | # (the "License"); you may not use this file except in compliance with |
---|
8 | # the License. You may obtain a copy of the License at |
---|
9 | # |
---|
10 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | # |
---|
12 | # Unless required by applicable law or agreed to in writing, software |
---|
13 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | # See the License for the specific language governing permissions and |
---|
16 | # limitations under the License. |
---|
17 | |
---|
18 | |
---|
19 | # Runs a HdfsProxy as a daemon. |
---|
20 | # |
---|
21 | # Environment Variables |
---|
22 | # |
---|
23 | # HDFSPROXY_CONF_DIR Alternate conf dir. Default is ${HDFSPROXY_HOME}/conf. |
---|
24 | # HDFSPROXY_LOG_DIR Where log files are stored. PWD by default. |
---|
25 | # HDFSPROXY_MASTER host:path where hdfsproxy code should be rsync'd from |
---|
26 | # HDFSPROXY_PID_DIR The pid files are stored. /tmp by default. |
---|
27 | # HDFSPROXY_IDENT_STRING A string representing this instance of hdfsproxy. $USER by default |
---|
28 | # HDFSPROXY_NICENESS The scheduling priority for daemons. Defaults to 0. |
---|
29 | ## |
---|
30 | |
---|
31 | usage="Usage: hdfsproxy-daemon.sh [--config <conf-dir>] [--hosts hostlistfile] (start|stop) " |
---|
32 | |
---|
33 | # if no args specified, show usage |
---|
34 | if [ $# -le 1 ]; then |
---|
35 | echo $usage |
---|
36 | exit 1 |
---|
37 | fi |
---|
38 | |
---|
39 | bin=`dirname "$0"` |
---|
40 | bin=`cd "$bin"; pwd` |
---|
41 | |
---|
42 | . "$bin"/hdfsproxy-config.sh |
---|
43 | |
---|
44 | # get arguments |
---|
45 | startStop=$1 |
---|
46 | shift |
---|
47 | |
---|
48 | hdfsproxy_rotate_log () |
---|
49 | { |
---|
50 | log=$1; |
---|
51 | num=5; |
---|
52 | if [ -n "$2" ]; then |
---|
53 | num=$2 |
---|
54 | fi |
---|
55 | if [ -f "$log" ]; then # rotate logs |
---|
56 | while [ $num -gt 1 ]; do |
---|
57 | prev=`expr $num - 1` |
---|
58 | [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num" |
---|
59 | num=$prev |
---|
60 | done |
---|
61 | mv "$log" "$log.$num"; |
---|
62 | fi |
---|
63 | } |
---|
64 | |
---|
65 | if [ -f "${HDFSPROXY_CONF_DIR}/hdfsproxy-env.sh" ]; then |
---|
66 | . "${HDFSPROXY_CONF_DIR}/hdfsproxy-env.sh" |
---|
67 | fi |
---|
68 | |
---|
69 | # get log directory |
---|
70 | if [ "$HDFSPROXY_LOG_DIR" = "" ]; then |
---|
71 | export HDFSPROXY_LOG_DIR="$HDFSPROXY_HOME/logs" |
---|
72 | fi |
---|
73 | mkdir -p "$HDFSPROXY_LOG_DIR" |
---|
74 | |
---|
75 | if [ "$HDFSPROXY_PID_DIR" = "" ]; then |
---|
76 | HDFSPROXY_PID_DIR=/tmp |
---|
77 | fi |
---|
78 | |
---|
79 | if [ "$HDFSPROXY_IDENT_STRING" = "" ]; then |
---|
80 | export HDFSPROXY_IDENT_STRING="$USER" |
---|
81 | fi |
---|
82 | |
---|
83 | # some variables |
---|
84 | export HDFSPROXY_LOGFILE=hdfsproxy-$HDFSPROXY_IDENT_STRING-$HOSTNAME.log |
---|
85 | export HDFSPROXY_ROOT_LOGGER="INFO,DRFA" |
---|
86 | log=$HDFSPROXY_LOG_DIR/hdfsproxy-$HDFSPROXY_IDENT_STRING-$HOSTNAME.out |
---|
87 | pid=$HDFSPROXY_PID_DIR/hdfsproxy-$HDFSPROXY_IDENT_STRING.pid |
---|
88 | |
---|
89 | # Set default scheduling priority |
---|
90 | if [ "$HDFSPROXY_NICENESS" = "" ]; then |
---|
91 | export HDFSPROXY_NICENESS=0 |
---|
92 | fi |
---|
93 | |
---|
94 | case $startStop in |
---|
95 | |
---|
96 | (start) |
---|
97 | |
---|
98 | mkdir -p "$HDFSPROXY_PID_DIR" |
---|
99 | |
---|
100 | if [ -f $pid ]; then |
---|
101 | if kill -0 `cat $pid` > /dev/null 2>&1; then |
---|
102 | echo hdfsproxy running as process `cat $pid`. Stop it first. |
---|
103 | exit 1 |
---|
104 | fi |
---|
105 | fi |
---|
106 | |
---|
107 | if [ "$HDFSPROXY_MASTER" != "" ]; then |
---|
108 | echo rsync from $HDFSPROXY_MASTER |
---|
109 | rsync -a -e ssh --delete --exclude=.svn --exclude='logs/*' --exclude='contrib/hod/logs/*' $HDFSPROXY_MASTER/ "$HDFSPROXY_HOME" |
---|
110 | fi |
---|
111 | |
---|
112 | hdfsproxy_rotate_log $log |
---|
113 | echo starting hdfsproxy, logging to $log |
---|
114 | cd "$HDFSPROXY_HOME" |
---|
115 | nohup nice -n $HDFSPROXY_NICENESS "$HDFSPROXY_HOME"/bin/hdfsproxy --config $HDFSPROXY_CONF_DIR "$@" > "$log" 2>&1 < /dev/null & |
---|
116 | echo $! > $pid |
---|
117 | sleep 1; head "$log" |
---|
118 | ;; |
---|
119 | |
---|
120 | (stop) |
---|
121 | |
---|
122 | if [ -f $pid ]; then |
---|
123 | if kill -0 `cat $pid` > /dev/null 2>&1; then |
---|
124 | echo stopping hdfsproxy |
---|
125 | kill `cat $pid` |
---|
126 | else |
---|
127 | echo no hdfsproxy to stop |
---|
128 | fi |
---|
129 | else |
---|
130 | echo no hdfsproxy to stop |
---|
131 | fi |
---|
132 | ;; |
---|
133 | |
---|
134 | (*) |
---|
135 | echo $usage |
---|
136 | exit 1 |
---|
137 | ;; |
---|
138 | |
---|
139 | esac |
---|
140 | |
---|
141 | |
---|