slaves.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Run a shell command on all slave hosts.
  17. #
  18. # Environment Variables
  19. #
  20. # HADOOP_SLAVES File naming remote hosts.
  21. # Default is ${HADOOP_CONF_DIR}/slaves.
  22. # HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_HOME}/conf.
  23. # HADOOP_SLAVE_SLEEP Seconds to sleep between spawning remote commands.
  24. # HADOOP_SSH_OPTS Options passed to ssh when running remote commands.
  25. ##
  26. usage="Usage: slaves.sh [--config confdir] command..."
  27. # if no args specified, show usage
  28. if [ $# -le 0 ]; then
  29. echo $usage
  30. exit 1
  31. fi
  32. bin=`dirname "$0"`
  33. bin=`cd "$bin"; pwd`
  34. if [ -e "$bin/../libexec/hadoop-config.sh" ]; then
  35. . "$bin"/../libexec/hadoop-config.sh
  36. else
  37. . "$bin/hadoop-config.sh"
  38. fi
  39. # If the slaves file is specified in the command line,
  40. # then it takes precedence over the definition in
  41. # hadoop-env.sh. Save it here.
  42. HOSTLIST=$HADOOP_SLAVES
  43. if [ -f "${HADOOP_CONF_DIR}/hadoop-env.sh" ]; then
  44. . "${HADOOP_CONF_DIR}/hadoop-env.sh"
  45. fi
  46. if [ "$HOSTLIST" = "" ]; then
  47. if [ "$HADOOP_SLAVES" = "" ]; then
  48. export HOSTLIST="${HADOOP_CONF_DIR}/slaves"
  49. else
  50. export HOSTLIST="${HADOOP_SLAVES}"
  51. fi
  52. fi
  53. for slave in `cat "$HOSTLIST"|sed "s/#.*$//;/^$/d"`; do
  54. ssh $HADOOP_SSH_OPTS $slave $"${@// /\\ }" \
  55. 2>&1 | sed "s/^/$slave: /" &
  56. if [ "$HADOOP_SLAVE_SLEEP" != "" ]; then
  57. sleep $HADOOP_SLAVE_SLEEP
  58. fi
  59. done
  60. wait