slaves.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env bash
  2. #
  3. # Run a shell command on all slave hosts.
  4. #
  5. # Environment Variables
  6. #
  7. # HADOOP_SLAVES File naming remote hosts.
  8. # Default is ${HADOOP_CONF_DIR}/slaves.
  9. # HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_HOME}/conf.
  10. # HADOOP_SLAVE_SLEEP Seconds to sleep between spawning remote commands.
  11. # HADOOP_SSH_OPTS Options passed to ssh when running remote commands.
  12. ##
  13. usage="Usage: slaves.sh [--config confdir] command..."
  14. # if no args specified, show usage
  15. if [ $# -le 0 ]; then
  16. echo $usage
  17. exit 1
  18. fi
  19. bin=`dirname "$0"`
  20. bin=`cd "$bin"; pwd`
  21. . "$bin"/hadoop-config.sh
  22. # If the slaves file is specified in the command line,
  23. # then it takes precedence over the definition in
  24. # hadoop-env.sh. Save it here.
  25. HOSTLIST=$HADOOP_SLAVES
  26. if [ -f "${HADOOP_CONF_DIR}/hadoop-env.sh" ]; then
  27. . "${HADOOP_CONF_DIR}/hadoop-env.sh"
  28. fi
  29. if [ "$HOSTLIST" = "" ]; then
  30. if [ "$HADOOP_SLAVES" = "" ]; then
  31. export HOSTLIST="${HADOOP_CONF_DIR}/slaves"
  32. else
  33. export HOSTLIST="${HADOOP_SLAVES}"
  34. fi
  35. fi
  36. for slave in `cat "$HOSTLIST"`; do
  37. ssh $HADOOP_SSH_OPTS $slave $"${@// /\\ }" \
  38. 2>&1 | sed "s/^/$slave: /" &
  39. if [ "$HADOOP_SLAVE_SLEEP" != "" ]; then
  40. sleep $HADOOP_SLAVE_SLEEP
  41. fi
  42. done
  43. wait