command_stop.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. fi
  30. sleep 1
  31. }
  32. # Attempts Graceful of goldsource using rcon 'quit' command.
  33. # Goldsource 'quit' command restarts rather than shutsdown
  34. # this function will only wait 3 seconds then force a tmux shutdown.
  35. # preventing the server from coming back online.
  36. fn_stop_graceful_goldsource(){
  37. fn_print_dots "Graceful: rcon quit"
  38. fn_scriptlog "Graceful: rcon quit"
  39. # sends quit
  40. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  41. # waits 3 seconds as goldsource servers restart with the quit command
  42. for seconds in {1..3}; do
  43. sleep 1
  44. fn_print_dots "Graceful: rcon quit: ${seconds}"
  45. done
  46. fn_print_ok_nl "Graceful: rcon quit: ${seconds}"
  47. sleep 1
  48. }
  49. # Attempts Graceful of 7 Days To Die using telnet.
  50. fn_stop_telnet_sdtd(){
  51. sdtdshutdown=$( expect -c '
  52. proc abort {} {
  53. puts "Timeout or EOF\n"
  54. exit 1
  55. }
  56. spawn telnet '"${telnetip}"' '"${telnetport}"'
  57. expect {
  58. "password:" { send "'"${telnetpass}"'\r" }
  59. default abort
  60. }
  61. expect {
  62. "session." { send "shutdown\r" }
  63. default abort
  64. }
  65. expect { eof }
  66. puts "Completed.\n"
  67. ')
  68. }
  69. fn_stop_graceful_sdtd(){
  70. # Gets server IP.
  71. info_config.sh
  72. fn_print_dots "Graceful: telnet"
  73. fn_scriptlog "Graceful: telnet"
  74. sleep 1
  75. # uses localhost on first attempt.
  76. telnetip=127.0.0.1
  77. fn_print_dots "Graceful: telnet: ${telnetip}"
  78. fn_scriptlog "Graceful: telnet: ${telnetip}"
  79. fn_stop_telnet_sdtd
  80. sleep 1
  81. # falls back to the server ip if localhost fails.
  82. refused=$(echo -en "\n ${sdtdshutdown}"| grep "Timeout or EOF")
  83. if [ -n "${refused}" ]; then
  84. fn_print_warn_nl "Graceful: telnet: localhost: "
  85. fn_print_fail_eol
  86. fn_scriptlog "Graceful: telnet: localhost: FAIL"
  87. sleep 1
  88. telnetip=${ip}
  89. fn_print_dots "Graceful: telnet: ${telnetip}"
  90. fn_scriptlog "Graceful: telnet: ${telnetip}"
  91. fn_stop_telnet_sdtd
  92. refused=$(echo -en "\n ${sdtdshutdown}"| grep "Timeout or EOF")
  93. fn_print_warnnl "Graceful: telnet: ${telnetip}: "
  94. fn_print_fail_eol
  95. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  96. sleep 1
  97. fi
  98. # Checks if attempts have worked.
  99. completed=$(echo -en "\n ${sdtdshutdown}"| grep "Completed.")
  100. if [ -n "${completed}" ]; then
  101. fn_print_ok_nl "Graceful: telnet: "
  102. fn_print_ok_eol
  103. fn_scriptlog "Graceful: telnet: OK"
  104. elif [ -n "${refused}" ]; then
  105. fn_print_fail_nl "Graceful: telnet: "
  106. fn_print_fail_eol
  107. fn_scriptlog "Graceful: telnet: ${telnetip}: FAIL"
  108. echo -en "\n\n" | tee -a "${scriptlog}"
  109. echo -en "Telnet output:" | tee -a "${scriptlog}"
  110. echo -en "\n ${sdtdshutdown}" | tee -a "${scriptlog}"
  111. echo -en "\n\n" | tee -a "${scriptlog}"
  112. else
  113. fn_print_fail_nl "Graceful: telnet: Unknown error"
  114. fn_scriptlog "Graceful: telnet: Unknown error"
  115. echo -en "\n\n" | tee -a "${scriptlog}"
  116. echo -en "Telnet output:" | tee -a "${scriptlog}"
  117. echo -en "\n ${sdtdshutdown}" | tee -a "${scriptlog}"
  118. echo -en "\n\n" | tee -a "${scriptlog}"
  119. fi
  120. }
  121. fn_stop_graceful_select(){
  122. if [ "${gamename}" == "7 Days to Die" ]; then
  123. fn_stop_graceful_sdtd
  124. elif [ "${engine}" == "source" ]; then
  125. fn_stop_graceful_source
  126. elif [ "${engine}" == "goldsource" ]; then
  127. fn_stop_graceful_goldsource
  128. else
  129. fn_stop_tmux
  130. fi
  131. }
  132. fn_stop_teamspeak3(){
  133. fn_print_dots "${servername}"
  134. fn_scriptlog "${servername}"
  135. sleep 1
  136. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  137. fn_print_ok "${servername}"
  138. fn_scriptlog "Stopped ${servername}"
  139. # Remove lock file
  140. rm -f "${rootdir}/${lockselfname}"
  141. sleep 1
  142. echo -en "\n"
  143. }
  144. fn_stop_tmux(){
  145. fn_print_dots "${servername}"
  146. fn_scriptlog "${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_stop_pre_check