您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

173 行
4.6 KiB

14 年前
  1. #!/bin/sh
  2. # -*- tab-width:4;indent-tabs-mode:nil -*-
  3. # ex: ts=4 sw=4 et
  4. RUNNER_SCRIPT_DIR=$(cd ${0%/*} && pwd)
  5. RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}
  6. RUNNER_ETC_DIR=$RUNNER_BASE_DIR/etc
  7. RUNNER_LOG_DIR=$RUNNER_BASE_DIR/log
  8. # Note the trailing slash on $PIPE_DIR/
  9. PIPE_DIR=/tmp/$RUNNER_BASE_DIR/
  10. RUNNER_USER=
  11. # Make sure this script is running as the appropriate user
  12. if [ ! -z "$RUNNER_USER" ] && [ `whoami` != "$RUNNER_USER" ]; then
  13. exec sudo -u $RUNNER_USER -i $0 $@
  14. fi
  15. # Make sure CWD is set to runner base dir
  16. cd $RUNNER_BASE_DIR
  17. # Make sure log directory exists
  18. mkdir -p $RUNNER_LOG_DIR
  19. # Extract the target node name from node.args
  20. NAME_ARG=`egrep '^-s?name' $RUNNER_ETC_DIR/vm.args`
  21. if [ -z "$NAME_ARG" ]; then
  22. echo "vm.args needs to have either -name or -sname parameter."
  23. exit 1
  24. fi
  25. # Extract the target cookie
  26. COOKIE_ARG=`grep '^-setcookie' $RUNNER_ETC_DIR/vm.args`
  27. if [ -z "$COOKIE_ARG" ]; then
  28. echo "vm.args needs to have a -setcookie parameter."
  29. exit 1
  30. fi
  31. # Identify the script name
  32. SCRIPT=`basename $0`
  33. # Parse out release and erts info
  34. START_ERL=`cat $RUNNER_BASE_DIR/releases/start_erl.data`
  35. ERTS_VSN=${START_ERL% *}
  36. APP_VSN=${START_ERL#* }
  37. # Add ERTS bin dir to our path
  38. ERTS_PATH=$RUNNER_BASE_DIR/erts-$ERTS_VSN/bin
  39. # Setup command to control the node
  40. NODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME_ARG $COOKIE_ARG"
  41. # Check the first argument for instructions
  42. case "$1" in
  43. start)
  44. # Make sure there is not already a node running
  45. RES=`$NODETOOL ping`
  46. if [ "$RES" = "pong" ]; then
  47. echo "Node is already running!"
  48. exit 1
  49. fi
  50. HEART_COMMAND="$RUNNER_BASE_DIR/bin/$SCRIPT start"
  51. export HEART_COMMAND
  52. mkdir -p $PIPE_DIR
  53. shift # remove $1
  54. $ERTS_PATH/run_erl -daemon $PIPE_DIR $RUNNER_LOG_DIR "exec $RUNNER_BASE_DIR/bin/$SCRIPT console $@" 2>&1
  55. ;;
  56. stop)
  57. # Wait for the node to completely stop...
  58. case `uname -s` in
  59. Linux|Darwin|FreeBSD|DragonFly|NetBSD|OpenBSD)
  60. # PID COMMAND
  61. PID=`ps ax -o pid= -o command=|\
  62. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  63. ;;
  64. SunOS)
  65. # PID COMMAND
  66. PID=`ps -ef -o pid= -o args=|\
  67. grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $1}'`
  68. ;;
  69. CYGWIN*)
  70. # UID PID PPID TTY STIME COMMAND
  71. PID=`ps -efW|grep "$RUNNER_BASE_DIR/.*/[b]eam"|awk '{print $2}'`
  72. ;;
  73. esac
  74. $NODETOOL stop
  75. ES=$?
  76. if [ "$ES" -ne 0 ]; then
  77. exit $ES
  78. fi
  79. while `kill -0 $PID 2>/dev/null`;
  80. do
  81. sleep 1
  82. done
  83. ;;
  84. restart)
  85. ## Restart the VM without exiting the process
  86. $NODETOOL restart
  87. ES=$?
  88. if [ "$ES" -ne 0 ]; then
  89. exit $ES
  90. fi
  91. ;;
  92. reboot)
  93. ## Restart the VM completely (uses heart to restart it)
  94. $NODETOOL reboot
  95. ES=$?
  96. if [ "$ES" -ne 0 ]; then
  97. exit $ES
  98. fi
  99. ;;
  100. ping)
  101. ## See if the VM is alive
  102. $NODETOOL ping
  103. ES=$?
  104. if [ "$ES" -ne 0 ]; then
  105. exit $ES
  106. fi
  107. ;;
  108. attach)
  109. # Make sure a node IS running
  110. RES=`$NODETOOL ping`
  111. ES=$?
  112. if [ "$ES" -ne 0 ]; then
  113. echo "Node is not running!"
  114. exit $ES
  115. fi
  116. shift
  117. exec $ERTS_PATH/to_erl $PIPE_DIR
  118. ;;
  119. console|console_clean)
  120. # .boot file typically just $SCRIPT (ie, the app name)
  121. # however, for debugging, sometimes start_clean.boot is useful:
  122. case "$1" in
  123. console) BOOTFILE=$SCRIPT ;;
  124. console_clean) BOOTFILE=start_clean ;;
  125. esac
  126. # Setup beam-required vars
  127. ROOTDIR=$RUNNER_BASE_DIR
  128. BINDIR=$ROOTDIR/erts-$ERTS_VSN/bin
  129. EMU=beam
  130. PROGNAME=`echo $0 | sed 's/.*\\///'`
  131. CMD="$BINDIR/erlexec -boot $RUNNER_BASE_DIR/releases/$APP_VSN/$BOOTFILE -mode embedded -config $RUNNER_ETC_DIR/app.config -args_file $RUNNER_ETC_DIR/vm.args -- ${1+"$@"}"
  132. export EMU
  133. export ROOTDIR
  134. export BINDIR
  135. export PROGNAME
  136. # Dump environment info for logging purposes
  137. echo "Exec: $CMD"
  138. echo "Root: $ROOTDIR"
  139. # Log the startup
  140. logger -t "$SCRIPT[$$]" "Starting up"
  141. # Start the VM
  142. exec $CMD
  143. ;;
  144. *)
  145. echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|console|console_clean|attach}"
  146. exit 1
  147. ;;
  148. esac
  149. exit 0