command_stop.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/bin/bash
  2. # LGSM command_stop.sh function
  3. # Author: Daniel Gibbs
  4. # Website: http://gameservermanagers.com
  5. lgsm_version="271215"
  6. # Description: Stops the server.
  7. local modulename="Stopping"
  8. function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  9. # Attempts Graceful of source using rcon 'quit' command.
  10. fn_stop_graceful_source(){
  11. fn_print_dots "Graceful: rcon quit"
  12. fn_scriptlog "Graceful: rcon quit"
  13. # sends quit
  14. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  15. # waits up to 30 seconds giving the server time to shutdown gracefuly
  16. for seconds in {1..30}; do
  17. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  18. if [ "${pid}" == "0" ]; then
  19. fn_print_ok_nl "Graceful: rcon quit: ${seconds}"
  20. fn_scriptlog "Graceful: rcon quit: OK: ${seconds} seconds"
  21. break
  22. fi
  23. sleep 1
  24. fn_print_dots "Graceful: rcon quit: ${seconds}"
  25. done
  26. if [ "${pid}" != "0" ]; then
  27. fn_print_fail_nl "Graceful: rcon quit"
  28. fn_scriptlog "Graceful: rcon quit: FAIL"
  29. fn_stop_tmux
  30. fi
  31. sleep 1
  32. }
  33. # Attempts Graceful of goldsource using rcon 'quit' command.
  34. # Goldsource 'quit' command restarts rather than shutsdown
  35. # this function will only wait 3 seconds then force a tmux shutdown.
  36. # preventing the server from coming back online.
  37. fn_stop_graceful_goldsource(){
  38. fn_print_dots "Graceful: rcon quit"
  39. fn_scriptlog "Graceful: rcon quit"
  40. # sends quit
  41. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  42. # waits 3 seconds as goldsource servers restart with the quit command
  43. for seconds in {1..3}; do
  44. sleep 1
  45. fn_print_dots "Graceful: rcon quit: ${seconds}"
  46. done
  47. fn_print_ok_nl "Graceful: rcon quit: ${seconds}"
  48. sleep 1
  49. fn_stop_tmux
  50. }
  51. # Attempts Graceful of 7 Days To Die using telnet.
  52. fn_stop_telnet_sdtd(){
  53. sdtdshutdown=$( expect -c '
  54. proc abort {} {
  55. puts "Timeout or EOF\n"
  56. exit 1
  57. }
  58. spawn telnet '"${telnetip}"' '"${telnetport}"'
  59. expect {
  60. "password:" { send "'"${telnetpass}"'\r" }
  61. default abort
  62. }
  63. expect {
  64. "session." { send "shutdown\r" }
  65. default abort
  66. }
  67. expect { eof }
  68. puts "Completed.\n"
  69. ')
  70. }
  71. fn_stop_graceful_sdtd(){
  72. # Gets server IP.
  73. info_config.sh
  74. fn_print_dots "Graceful: telnet"
  75. fn_scriptlog "Graceful: telnet"
  76. sleep 1
  77. # uses localhost on first attempt.
  78. telnetip=127.0.0.1
  79. fn_print_dots "Graceful: telnet: ${telnetip}"
  80. fn_scriptlog "Graceful: telnet: ${telnetip}"
  81. fn_stop_telnet_sdtd
  82. sleep 1
  83. # falls back to the server ip if localhost fails.
  84. refused=$(echo -en "\n ${sdtdshutdown}"| grep "Timeout or EOF")
  85. if [ -n "${refused}" ]; then
  86. fn_print_warn_nl "Graceful: telnet: localhost: "
  87. fn_print_fail_eol
  88. fn_scriptlog "Graceful: telnet: localhost: FAIL"
  89. sleep 1
  90. telnetip=${ip}
  91. fn_print_dots "Graceful: telnet: ${telnetip}"
  92. fn_scriptlog "Graceful: telnet: ${telnetip}"
  93. fn_stop_telnet_sdtd
  94. refused=$(echo -en "\n ${sdtdshutdown}"| grep "Timeout or EOF")
  95. fn_print_warnnl "Graceful: telnet: ${telnetip}: "
  96. fn_print_fail_eol
  97. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  98. sleep 1
  99. fi
  100. # Checks if attempts have worked.
  101. completed=$(echo -en "\n ${sdtdshutdown}"| grep "Completed.")
  102. if [ -n "${completed}" ]; then
  103. fn_print_ok_nl "Graceful: telnet: "
  104. fn_print_ok_eol
  105. fn_scriptlog "Graceful: telnet: OK"
  106. elif [ -n "${refused}" ]; then
  107. fn_print_fail_nl "Graceful: telnet: "
  108. fn_print_fail_eol
  109. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  110. echo -en "\n\n" | tee -a "${scriptlog}"
  111. echo -en "Telnet output:" | tee -a "${scriptlog}"
  112. echo -en "\n ${sdtdshutdown}" | tee -a "${scriptlog}"
  113. echo -en "\n\n" | tee -a "${scriptlog}"
  114. else
  115. fn_print_fail_nl "Graceful: telnet: Unknown error"
  116. fn_scriptlog "Graceful: telnet: Unknown error"
  117. echo -en "\n\n" | tee -a "${scriptlog}"
  118. echo -en "Telnet output:" | tee -a "${scriptlog}"
  119. echo -en "\n ${sdtdshutdown}" | tee -a "${scriptlog}"
  120. echo -en "\n\n" | tee -a "${scriptlog}"
  121. fi
  122. }
  123. fn_stop_graceful_select(){
  124. if [ "${gamename}" == "7 Days to Die" ]; then
  125. fn_stop_graceful_sdtd
  126. elif [ "${engine}" == "source" ]; then
  127. fn_stop_graceful_source
  128. elif [ "${engine}" == "goldsource" ]; then
  129. fn_stop_graceful_goldsource
  130. else
  131. fn_stop_tmux
  132. fi
  133. }
  134. fn_stop_teamspeak3(){
  135. fn_print_dots "${servername}"
  136. fn_scriptlog "${servername}"
  137. sleep 1
  138. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  139. # Remove lock file
  140. rm -f "${rootdir}/${lockselfname}"
  141. fn_print_ok_nl "${servername}"
  142. fn_scriptlog "Stopped ${servername}"
  143. }
  144. fn_stop_tmux(){
  145. fn_print_dots "${servername}"
  146. fn_scriptlog "tmux kill-session: ${servername}"
  147. sleep 1
  148. # Kill tmux session
  149. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  150. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  151. if [ "${pid}" == "0" ]; then
  152. fn_print_ok_nl "${servername}"
  153. fn_scriptlog "Stopped ${servername}"
  154. sleep 1
  155. # Remove lock file
  156. rm -f "${rootdir}/${lockselfname}"
  157. else
  158. fn_print_fail_nl "Unable to stop${servername}"
  159. fn_scriptlog "Unable to stop${servername}"
  160. fi
  161. }
  162. # checks if the server is already stopped before trying to stop.
  163. fn_stop_pre_check(){
  164. if [ "${gamename}" == "Teamspeak 3" ]; then
  165. info_ts3status.sh
  166. if [ "${ts3status}" = "No server running (ts3server.pid is missing)" ]; then
  167. fn_print_ok_nl "${servername} is already stopped"
  168. fn_scriptlog "${servername} is already stopped"
  169. else
  170. fn_stop_teamspeak3
  171. fi
  172. else
  173. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  174. if [ "${pid}" == "0" ]; then
  175. fn_print_ok_nl "${servername} is already stopped"
  176. fn_scriptlog "${servername} is already stopped"
  177. else
  178. fn_stop_graceful_select
  179. fi
  180. fi
  181. }
  182. check.sh
  183. fn_print_dots "${servername}"
  184. fn_scriptlog "${servername}"
  185. sleep 1
  186. fn_stop_pre_check