hadoop-daemon.sh 2.2 KB

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