athena-start 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. PSRUN=$(pgrep -F ${PIDFILE})
  20. fi
  21. #return ${PSRUN} #seems to cause an issue for some os
  22. }
  23. #checking if already started, launch and mark in log
  24. start_serv(){
  25. get_status $1
  26. if [ $2 ]; then #is logging on ?
  27. LOGFILE="$LOG_DIR/$1.launch.log"
  28. LOGRUN="$LOG_DIR/$1.log"
  29. FIFO="$1_fifo"
  30. echo "stat_serv, log is enabled"
  31. echo "My logfile=${LOGFILE}"
  32. if [ -z ${PSRUN} ]; then
  33. if [ -e ./${FIFO} ]; then rm "$FIFO"; fi
  34. mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$1" > "$FIFO" 2>&1 & PID=$!
  35. #"./$1" > >(tee "$LOGRUN") 2>&1 & PID=$! #bash only
  36. echo "$PID" > .$1.pid
  37. echo "Server '$1' started at $(date +"%m-%d-%H:%M-%S")" | tee ${LOGFILE}
  38. else
  39. echo "Cannot start '$1', because it is already running p${PSRUN}" | tee ${LOGFILE}
  40. fi
  41. else
  42. if [ -z ${PSRUN} ]; then
  43. ./$1&
  44. echo "$!" > .$1.pid
  45. echo "Server '$1' started at $(date +"%m-%d-%H:%M-%S")"
  46. else
  47. echo "Cannot start '$1', because it is already running p${PSRUN}"
  48. fi
  49. fi
  50. }
  51. watch_serv(){
  52. ulimit -Sc unlimited
  53. #now checking status and looping
  54. count=0;
  55. while true; do
  56. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  57. do
  58. LOGFILE="$LOG_DIR/$i.launch.log"
  59. LOGRUN="$LOG_DIR/$i.log"
  60. FIFO=$i"_fifo"
  61. get_status ${i}
  62. #echo "Echo id of $i is ${PSRUN}"
  63. if [ -z ${PSRUN} ]; then
  64. count=$((count+1))
  65. #echo "fifo=$FIFO"
  66. echo "server '$i' is down"
  67. echo "server '$i' is down" >> ${LOGFILE}
  68. echo "restarting server at time at $(date +"%m-%d-%H:%M-%S")"
  69. echo "restarting server at time at $(date +"%m-%d-%H:%M-%S")" >> ${LOGFILE}
  70. if [ -e $FIFO ]; then rm $FIFO; fi
  71. mkfifo "$FIFO"; tee "$LOGRUN" < "$FIFO" & "./$i" > "$FIFO" 2>&1 & PID=$!
  72. echo "$PID" > .$i.pid
  73. if [ $2 ] && [ $2 -lt $count ]; then break; fi
  74. fi
  75. done
  76. sleep $1
  77. done
  78. }
  79. restart(){
  80. $0 stop
  81. if [ $1 ]; then sleep $1; fi
  82. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  83. do
  84. FIFO="$1_fifo"
  85. while true; do
  86. get_status ${i}
  87. if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting for the process to end"; sleep 2;
  88. else
  89. if [ -e ./${FIFO} ]; then rm "$FIFO"; fi
  90. break
  91. fi
  92. done
  93. done
  94. $0 start
  95. }
  96. case $1 in
  97. 'start')
  98. print_start
  99. check_files
  100. echo "Check complete."
  101. echo "Looks like a good, nice rAthena!"
  102. if [ "$2" = "--enlog" ]; then
  103. ENLOG=1
  104. if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
  105. echo "Logging is enabled in $LOG_DIR"
  106. else
  107. echo "Logging is disabled"
  108. fi
  109. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  110. do
  111. start_serv $i $ENLOG
  112. done
  113. echo "rAthena was started."
  114. ;;
  115. 'watch')
  116. if [ ! -d "$LOG_DIR" ]; then mkdir -p $LOG_DIR; fi
  117. if [ -z $2 ]; then Restart_count=10; else Restart_count=$2; fi
  118. if [ -z $3 ]; then Restart_sleep=3; else Restart_sleep=$3; fi
  119. echo "Going to watch rAthena for restart_count = $Restart_count, restart_sleep = $Restart_sleep"
  120. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  121. do
  122. start_serv $i 1
  123. done
  124. watch_serv $Restart_count $Restart_sleep
  125. echo "Watching rAthena now."
  126. ;;
  127. 'stop')
  128. for i in ${W_SRV} ${M_SRV} ${C_SRV} ${L_SRV}
  129. do
  130. PIDFILE=.${i}.pid
  131. if [ -e ./${PIDFILE} ]; then
  132. kill $(cat ${PIDFILE})
  133. while true; do
  134. get_status ${i}
  135. if [ ${PSRUN} ]; then echo "'${i}' is still running p${PSRUN} waiting for the process to end"; sleep 2;
  136. else
  137. break
  138. fi
  139. done
  140. rm ${PIDFILE}
  141. fi
  142. done
  143. ;;
  144. 'restart')
  145. restart "$@"
  146. ;;
  147. 'status')
  148. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  149. do
  150. get_status ${i}
  151. if [ ${PSRUN} ]; then echo "'${i}' is running p${PSRUN}"; else echo "'${i}' seems to be down"; fi
  152. done
  153. ;;
  154. 'val_runonce')
  155. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  156. do
  157. valgrind --leak-check=full --show-leak-kinds=all ./$i --run-once > "log/$i.runonce.leak"
  158. done
  159. ;;
  160. 'valchk')
  161. for i in ${L_SRV} ${C_SRV} ${M_SRV} ${W_SRV}
  162. do
  163. valgrind --leak-check=full --show-leak-kinds=all ./$i > "log/$i.runonce.leak"
  164. done
  165. ;;
  166. 'help')
  167. case $2 in
  168. 'start')
  169. echo "syntax: 'start {--enlog}'"
  170. echo "This option will start the servers"
  171. echo "--enlog will write all terminal output into a log/\$servname.log file"
  172. ;;
  173. 'stop')
  174. echo "This option will shut the servers down"
  175. ;;
  176. 'restart')
  177. echo "syntax: 'restart {<delay>}'"
  178. echo "This option will wait for the given delay and will attempt to restart the servers afterwards"
  179. echo "Note: Even if the delay is over it will wait until the pid is finished before attempting to restart the servers"
  180. ;;
  181. 'status')
  182. echo "syntax: 'status'"
  183. echo "This option will let you know whether the server are running or not"
  184. echo "Note: This option is based on PID and requires that you have launched the servers with this script too"
  185. echo "If this was not the case please use something like 'ps ax | grep server' to check their status"
  186. ;;
  187. 'watch')
  188. echo "syntax: 'watch {<restart_interval> <restart_count>}'"
  189. echo "The watch option allows you to automatically restart the servers when one of them was stopped"
  190. echo "<restart_interval> delay in seconds before rechecking if a server is down (default 10) "
  191. echo "<restart_count> how many times the servers should be restarted (default 3), (-1=indefinitly)"
  192. ;;
  193. 'val_runonce')
  194. echo "syntax: 'val_runonce'"
  195. echo "This option will run valgrind with run-once to check the servers"
  196. ;;
  197. 'valchk')
  198. echo "syntax: 'valchk'"
  199. echo "This option will run valgrind with the servers"
  200. ;;
  201. *)
  202. echo "Please specify a command you would like more info on { start | stop | restart | status | watch }"
  203. read -p "Enter a valid command: " readEnterKey
  204. $0 "help" $readEnterKey
  205. ;;
  206. esac
  207. ;;
  208. *)
  209. echo "Usage: athena-start { start | stop | restart | status | watch | help | val_runonce | valchk }"
  210. read -p "Enter a valid option: " readEnterKey
  211. $0 $readEnterKey
  212. ;;
  213. esac