hadoop-daemon.sh 2.0 KB

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