athena-start 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "rAthena is starting..."
  10. echo " (c) 2021 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 $1 }')
  21. fi
  22. #return ${PSRUN} #seems to cause an issue for some os
  23. }
  24. #checking 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 "Cannot start '$1', because it 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 "Cannot start '$1', because it 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} ${W_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} ${W_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 for the process to 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 like a good, nice rAthena!"
  103. if [ "$2" = "--enlog" ]; then
  104. ENLOG=1
  105. if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
  106. echo "Logging is enabled in $LOG_DIR"
  107. else
  108. echo "Logging is disabled"
  109. fi
  110. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  111. do
  112. start_serv $i $ENLOG
  113. done
  114. echo "rAthena was started."
  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 "Going to watch rAthena for restart_count = $Restart_count, restart_sleep = $Restart_sleep"
  121. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  122. do
  123. start_serv $i 1
  124. done
  125. watch_serv $Restart_count $Restart_sleep
  126. echo "Watching rAthena now."
  127. ;;
  128. 'stop')
  129. for i in ${W_SRV} ${M_SRV} ${C_SRV} ${L_SRV}
  130. do
  131. PIDFILE=.${i}.pid
  132. if [ -e ./${PIDFILE} ]; then
  133. kill $(cat ${PIDFILE})
  134. while true; do
  135. get_status ${i}
  136. if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting for the process to end"; sleep 2;
  137. else
  138. break
  139. fi
  140. done
  141. rm ${PIDFILE}
  142. fi
  143. done
  144. ;;
  145. 'restart')
  146. restart
  147. ;;
  148. 'status')
  149. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  150. do
  151. get_status ${i}
  152. if [ ${PSRUN} ]; then echo "'${i}' is running p${PSRUN}"; else echo "'${i}' seems to be down"; fi
  153. done
  154. ;;
  155. 'val_runonce')
  156. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  157. do
  158. valgrind --leak-check=full --show-leak-kinds=all ./$i --run-once > "log/$i.runonce.leak"
  159. done
  160. ;;
  161. 'valchk')
  162. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  163. do
  164. valgrind --leak-check=full --show-leak-kinds=all ./$i > "log/$i.runonce.leak"
  165. done
  166. ;;
  167. 'help')
  168. case $2 in
  169. 'start')
  170. echo "syntax: 'start {--enlog}'"
  171. echo "This option will start the servers"
  172. echo "--enlog will write all terminal output into a log/$servname.log file"
  173. ;;
  174. 'stop')
  175. echo "This option will shut the servers down"
  176. ;;
  177. 'restart')
  178. echo "syntax: 'restart {<delay>}'"
  179. echo "This option will wait for the given delay and will attempt to restart the servers afterwards"
  180. echo "Note: Even if the delay is over it will wait until the pid is finished before attempting to restart the servers"
  181. ;;
  182. 'status')
  183. echo "syntax: 'status'"
  184. echo "This option will let you know whether the server are running or not"
  185. echo "Note: This option is based on PID and requires that you have launched the servers with this script too"
  186. echo "If this was not the case please use something like 'ps ax | grep server' to check their status"
  187. ;;
  188. 'watch')
  189. echo "syntax: 'watch {<restart_interval> <restart_count>}'"
  190. echo "The watch option allows you to automatically restart the servers when one of them was stopped"
  191. echo "<restart_interval> delay in seconds before rechecking if a server is down (default 10) "
  192. echo "<restart_count> how many times the servers should be restarted (default 3), (-1=indefinitly)"
  193. ;;
  194. 'val_runonce')
  195. echo "syntax: 'val_runonce'"
  196. echo "This option will run valgrin with run-once to check the servers"
  197. ;;
  198. 'valchk')
  199. echo "syntax: 'valchk'"
  200. echo "This option will run valgrin with the servers"
  201. ;;
  202. *)
  203. echo "Please specify a command you would like more info on { start | stop | restart | status | watch }"
  204. read -p "Enter a valid command: " readEnterKey
  205. $0 "help" $readEnterKey
  206. ;;
  207. esac
  208. ;;
  209. *)
  210. echo "Usage: athena-start { start | stop | restart | status | watch | help | val_runonce | valchk }"
  211. read -p "Enter a valid option: " readEnterKey
  212. $0 $readEnterKey
  213. ;;
  214. esac