hadoop-daemon.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. root=`dirname $this`/..
  36. # get log directory
  37. if [ "$HADOOP_LOG_DIR" = "" ]; then
  38. HADOOP_LOG_DIR=$root/logs
  39. mkdir -p $HADOOP_LOG_DIR
  40. fi
  41. if [ "$HADOOP_PID_DIR" = "" ]; then
  42. HADOOP_PID_DIR=/tmp
  43. fi
  44. if [ "$HADOOP_IDENT_STRING" = "" ]; then
  45. HADOOP_IDENT_STRING=$USER
  46. fi
  47. # some variables
  48. log=$HADOOP_LOG_DIR/hadoop-$HADOOP_IDENT_STRING-$command-`hostname`.log
  49. pid=$HADOOP_PID_DIR/hadoop-$HADOOP_IDENT_STRING-$command.pid
  50. case $startStop in
  51. (start)
  52. if [ -f $pid ]; then
  53. if [ -a /proc/`cat $pid` ]; then
  54. echo $command running as process `cat $pid`. Stop it first.
  55. exit 1
  56. fi
  57. fi
  58. if [ "$HADOOP_MASTER" != "" ]; then
  59. echo rsync from $HADOOP_MASTER
  60. rsync -a --delete --exclude=.svn $HADOOP_MASTER/ $root
  61. fi
  62. cd $root
  63. echo starting $command, logging to $log
  64. nohup bin/hadoop $command "$@" >& $log < /dev/null &
  65. echo $! > $pid
  66. sleep 1; head $log
  67. ;;
  68. (stop)
  69. if [ -f $pid ]; then
  70. if [ -a /proc/`cat $pid` ]; then
  71. echo stopping $command
  72. kill `cat $pid`
  73. else
  74. echo no $command to stop
  75. fi
  76. else
  77. echo no $command to stop
  78. fi
  79. ;;
  80. (*)
  81. echo $usage
  82. exit 1
  83. ;;
  84. esac