hadoop-daemon.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. #
  3. # Runs a Hadoop command as a daemon.
  4. #
  5. # Environment Variables
  6. #
  7. # HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_HOME}/conf.
  8. # HADOOP_LOG_DIR Where log files are stored. PWD by default.
  9. # HADOOP_MASTER host:path where hadoop code should be rsync'd from
  10. # HADOOP_PID_DIR The pid files are stored. /tmp by default.
  11. # HADOOP_IDENT_STRING A string representing this instance of hadoop. $USER by default
  12. ##
  13. usage="Usage: hadoop-daemon [start|stop] [hadoop-command] [args...]"
  14. # if no args specified, show usage
  15. if [ $# -le 1 ]; then
  16. echo $usage
  17. exit 1
  18. fi
  19. # get arguments
  20. startStop=$1
  21. shift
  22. command=$1
  23. shift
  24. # resolve links - $0 may be a softlink
  25. this="$0"
  26. while [ -h "$this" ]; do
  27. ls=`ls -ld "$this"`
  28. link=`expr "$ls" : '.*-> \(.*\)$'`
  29. if expr "$link" : '.*/.*' > /dev/null; then
  30. this="$link"
  31. else
  32. this=`dirname "$this"`/"$link"
  33. fi
  34. done
  35. # the root of the Hadoop installation
  36. HADOOP_HOME=`dirname "$this"`/..
  37. # Allow alternate conf dir location.
  38. HADOOP_CONF_DIR="${HADOOP_CONF_DIR:-$HADOOP_HOME/conf}"
  39. if [ -f "${HADOOP_CONF_DIR}/hadoop-env.sh" ]; then
  40. source "${HADOOP_CONF_DIR}/hadoop-env.sh"
  41. fi
  42. # get log directory
  43. if [ "$HADOOP_LOG_DIR" = "" ]; then
  44. HADOOP_LOG_DIR="$HADOOP_HOME/logs"
  45. mkdir -p "$HADOOP_LOG_DIR"
  46. fi
  47. if [ "$HADOOP_PID_DIR" = "" ]; then
  48. HADOOP_PID_DIR=/tmp
  49. fi
  50. if [ "$HADOOP_IDENT_STRING" = "" ]; then
  51. HADOOP_IDENT_STRING=$USER
  52. fi
  53. # some variables
  54. log=$HADOOP_LOG_DIR/hadoop-$HADOOP_IDENT_STRING-$command-`hostname`.log
  55. pid=$HADOOP_PID_DIR/hadoop-$HADOOP_IDENT_STRING-$command.pid
  56. case $startStop in
  57. (start)
  58. if [ -f $pid ]; then
  59. if [ -a /proc/`cat $pid` ]; then
  60. echo $command running as process `cat $pid`. Stop it first.
  61. exit 1
  62. fi
  63. fi
  64. if [ "$HADOOP_MASTER" != "" ]; then
  65. echo rsync from $HADOOP_MASTER
  66. rsync -a -e ssh --delete --exclude=.svn $HADOOP_MASTER/ "$HADOOP_HOME"
  67. fi
  68. cd "$HADOOP_HOME"
  69. echo starting $command, logging to $log
  70. nohup bin/hadoop $command "$@" >& "$log" < /dev/null &
  71. echo $! > $pid
  72. sleep 1; head "$log"
  73. ;;
  74. (stop)
  75. if [ -f $pid ]; then
  76. if [ -a /proc/`cat $pid` ]; then
  77. echo stopping $command
  78. kill `cat $pid`
  79. else
  80. echo no $command to stop
  81. fi
  82. else
  83. echo no $command to stop
  84. fi
  85. ;;
  86. (*)
  87. echo $usage
  88. exit 1
  89. ;;
  90. esac