athena-start 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/sh
  2. #source var/function
  3. . ./function.sh
  4. inst_launch_workaround
  5. PATH=./:$PATH
  6. LOG_DIR="./log"
  7. print_start() {
  8. # more << EOF
  9. echo "Athena Starting..."
  10. echo " (c) 2013 rAthena Project"
  11. echo ""
  12. echo ""
  13. echo "checking..."
  14. #EOF
  15. }
  16. get_status(){
  17. PIDFILE=.$1.pid
  18. if [ -e ${PIDFILE} ]; then
  19. ISRUN=$(ps ax | grep $(cat ${PIDFILE}) | grep $1)
  20. PSRUN=$(echo "$ISRUN" | awk '{ print substr( $0, 0, 7) }')
  21. fi
  22. #return ${PSRUN} #seem to cause issue for some os
  23. }
  24. #cheking if already started, launch and mark in log
  25. start_serv(){
  26. get_status $1
  27. if [ $2 ]; then #is logging on ?
  28. LOGFILE="$LOG_DIR/$1.launch.log"
  29. LOGRUN="$LOG_DIR/$1.log"
  30. FIFO="$1_fifo"
  31. echo "stat_serv, log is enabled"
  32. echo "My logfile=${LOGFILE}"
  33. if [ -z ${PSRUN} ]; then
  34. if [ -e ./${FIFO} ]; then rm "$FIFO"; fi
  35. mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$1" > "$FIFO" 2>&1 & PID=$!
  36. #"./$1" > >(tee "$LOGRUN") 2>&1 & PID=$! #bash only
  37. echo "$PID" > .$1.pid
  38. echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`" | tee ${LOGFILE}
  39. else
  40. echo "Can't start '$1', cause is already running p${PSRUN}" | tee ${LOGFILE}
  41. fi
  42. else
  43. if [ -z ${PSRUN} ]; then
  44. ./$1&
  45. echo "$!" > .$1.pid
  46. echo "Server '$1' started at `date +"%m-%d-%H:%M-%S"`"
  47. else
  48. echo "Can't start '$1', cause is already running p${PSRUN}"
  49. fi
  50. fi
  51. }
  52. watch_serv(){
  53. ulimit -Sc unlimited
  54. #now checking status and looping
  55. count=0;
  56. while true; do
  57. for i in ${L_SRV} ${C_SRV} ${M_SRV}
  58. do
  59. LOGFILE="$LOG_DIR/$i.launch.log"
  60. LOGRUN="$LOG_DIR/$i.log"
  61. FIFO=$i"_fifo"
  62. get_status $i
  63. #echo "Echo id of $i is ${PSRUN}"
  64. if [ -z ${PSRUN} ]; then
  65. count=$((count+1))
  66. #echo "fifo=$FIFO"
  67. echo "server '$i' is down"
  68. echo "server '$i' is down" >> ${LOGFILE}
  69. echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`"
  70. echo "restarting server at time at `date +"%m-%d-%H:%M-%S"`" >> ${LOGFILE}
  71. if [ -e $FIFO ]; then rm $FIFO; fi
  72. mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$i" > "$FIFO" 2>&1 & PID=$!
  73. echo "$PID" > .$i.pid
  74. if [ $2 ] && [ $2 -lt $count ]; then break; fi
  75. fi
  76. done
  77. sleep $1
  78. done
  79. }
  80. restart(){
  81. $0 stop
  82. if [ $1 ]; then sleep $1; fi
  83. for i in ${L_SRV} ${C_SRV} ${M_SRV}
  84. do
  85. FIFO="$1_fifo"
  86. while true; do
  87. get_status ${i}
  88. if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting end"; sleep 2;
  89. else
  90. if [ -e ./${FIFO} ]; then rm "$FIFO"; fi
  91. break
  92. fi
  93. done
  94. done
  95. $0 start
  96. }
  97. case $1 in
  98. 'start')
  99. print_start
  100. check_files
  101. echo "Check complete."
  102. echo "Looks good, a nice Athena!"
  103. if [ "$2" = "--enlog" ]; then
  104. ENLOG=1
  105. if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
  106. echo "Logging is enable in $LOG_DIR"
  107. else
  108. echo "Logging is disable"
  109. fi
  110. for i in ${L_SRV} ${C_SRV} ${M_SRV}
  111. do
  112. start_serv $i $ENLOG
  113. done
  114. echo "Now Started Athena."
  115. ;;
  116. 'watch')
  117. if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
  118. if [ -z $2 ]; then Restart_count=10; else Restart_count=$2; fi
  119. if [ -z $3 ]; then Restart_sleep=3; else Restart_sleep=$3; fi
  120. echo " Gonna watch rA for Restart_count = $Restart_count, Restart_sleep= $Restart_sleep"
  121. for i in ${L_SRV} ${C_SRV} ${M_SRV}
  122. do
  123. start_serv $i 1
  124. done
  125. watch_serv $Restart_count $Restart_sleep
  126. echo "Now watching Athena."
  127. ;;
  128. 'stop')
  129. for i in ${L_SRV} ${C_SRV} ${M_SRV}
  130. do
  131. PIDFILE=.${i}.pid
  132. if [ -e ./${PIDFILE} ]; then
  133. kill $(cat ${PIDFILE})
  134. rm ${PIDFILE}
  135. fi
  136. done
  137. ;;
  138. 'restart')
  139. restart
  140. ;;
  141. 'status')
  142. for i in ${L_SRV} ${C_SRV} ${M_SRV}
  143. do
  144. get_status ${i}
  145. if [ ${PSRUN} ]; then echo "'${i}' is running p${PSRUN}"; else echo "'${i}' seem down"; fi
  146. done
  147. ;;
  148. 'help')
  149. case $2 in
  150. 'start')
  151. echo "syntaxe: 'start {--enlog}'"
  152. echo "This option will starts the servs"
  153. echo "--enlog will tee all terminal output into a log/$servname.log file"
  154. ;;
  155. 'stop')
  156. echo "This option will shutdowns the servs'"
  157. ;;
  158. 'restart')
  159. echo "syntaxe: 'restart {<delay>}'"
  160. echo "This option will wait delay then will attempt to restart the servs"
  161. echo "NB, even if delay is over it will wait the pid is finished before atetmpting to restart servs"
  162. ;;
  163. 'status')
  164. echo "This option let you know if the server are running or not"
  165. echo "NB this option is based on PID and supposed you have launch the serv by this script"
  166. echo " If it wasn't the case please use something like 'ps ax | grep server' to know their status"
  167. ;;
  168. 'watch')
  169. echo "syntaxe: 'watch {<restart_intervall> <restart_count>}'"
  170. echo "The watch option allow you to auto restart the server when this one was stopped"
  171. echo "<restart_intervall> delay in second before recheking if server are down (default 10) "
  172. echo "<restart_count> how many time should we restart servs (default 3), (-1=undefinitly)"
  173. ;;
  174. *)
  175. echo "Please specify a command you'll like more info { start | stop | restart | status | watch }"
  176. read -p "Enter a valid command: " readEnterKey
  177. $0 "help" $readEnterKey
  178. ;;
  179. esac
  180. ;;
  181. *)
  182. echo "Usage: athena-start { start | stop | restart | status | watch | help }"
  183. read -p "Enter a valid option: " readEnterKey
  184. $0 $readEnterKey
  185. ;;
  186. esac