command_stop.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. sdtd_telnet(){
  10. sdtdshutdown=$( expect -c '
  11. proc abort {} {
  12. puts "Timeout or EOF\n"
  13. exit 1
  14. }
  15. spawn telnet '"${telnetip}"' '"${telnetport}"'
  16. expect {
  17. "password:" { send "'"${telnetpass}"'\r" }
  18. default abort
  19. }
  20. expect {
  21. "session." { send "shutdown\r" }
  22. default abort
  23. }
  24. expect { eof }
  25. puts "Completed.\n"
  26. ')
  27. }
  28. fn_stop_ark(){
  29. fn_printwarn "attempting to determine any in-use ports"
  30. fn_scriptlog "attempting to determine any in-use ports"
  31. info_config.sh
  32. if [ -z $queryport ] ; then
  33. fn_printwarn "no queryport found in the arkserver script"
  34. fn_scriptlog "no queryport found in the arkserver script"
  35. userconfigfile="${filesdir}"
  36. userconfigfile+="/ShooterGame/Saved/Config/LinuxServer/GameUserSettings.ini"
  37. port=$(grep ^QueryPort= ${userconfigfile} | cut -d= -f2 | sed "s/[^[:digit:].*].*//g")
  38. fi
  39. if [ -z $queryport ] ; then
  40. fn_printwarn "no queryport found in the GameUsersettings.ini file"
  41. fn_scriptlog "no queryport found in the GameUsersettings.ini file"
  42. return
  43. fi
  44. if [[ ${#port} -gt 0 ]] ; then
  45. fn_printwarn "QueryPort $queryport found in the configs. Checking to see if it's still bound."
  46. fn_scriptlog "QueryPort $queryport found in the configs. Checking to see if it's still bound."
  47. pid=$(netstat -nap 2>/dev/null | grep ^udp[[:space:]] |\
  48. grep :${queryport}[[:space:]] | rev | awk '{print $1}' |\
  49. rev | cut -d\/ -f1)
  50. #
  51. # check for a valid pid
  52. let pid+=0 # turns an empty string into a valid number, '0',
  53. # and a valid numeric pid remains unchanged.
  54. if [[ $pid -gt 1 && $pid -le $(cat /proc/sys/kernel/pid_max) ]] ; then
  55. # kill the process still listening on that port.
  56. fn_printdots "Killing process $pid"
  57. fn_scriptlog "Killing process $pid"
  58. kill -9 $pid
  59. fi # end if for pid range check
  60. fn_printok "${servername} stopped and ports cleared"
  61. fn_scriptlog "Ports cleared for ${servername}"
  62. fi # end if for port check
  63. } # end of fn_stop_ark
  64. fn_stop_teamspeak3(){
  65. check.sh
  66. fn_printdots "${servername}"
  67. fn_scriptlog "${servername}"
  68. sleep 1
  69. info_ts3status.sh
  70. if [ "${ts3status}" = "No server running (ts3server.pid is missing)" ]; then
  71. fn_printfail "${servername} is already stopped"
  72. fn_scriptlog "${servername} is already stopped"
  73. else
  74. ${filesdir}/ts3server_startscript.sh stop > /dev/null 2>&1
  75. fn_printok "${servername}"
  76. fn_scriptlog "Stopped ${servername}"
  77. fi
  78. # Remove lock file
  79. rm -f "${rootdir}/${lockselfname}"
  80. sleep 1
  81. echo -en "\n"
  82. }
  83. fn_stop_tmux(){
  84. check.sh
  85. info_config.sh
  86. fn_printdots "${servername}"
  87. fn_scriptlog "${servername}"
  88. sleep 1
  89. if [ "${gamename}" == "7 Days To Die" ] ; then
  90. # if game is 7 Days To Die, we need special, graceful shutdown via telnet connection.
  91. # Set below variable to be called for expect to operate correctly..
  92. fn_printdots "Attempting graceful shutdown via telnet"
  93. fn_scriptlog "Attempting graceful shutdown via telnet"
  94. sleep 1
  95. telnetip=127.0.0.1
  96. sdtd_telnet
  97. # If failed using localhost will use servers ip
  98. refused=$(echo -en "\n ${sdtdshutdown}"| grep "Timeout or EOF")
  99. if [ -n "${refused}" ]; then
  100. telnetip=${ip}
  101. fn_printwarn "Attempting graceful shutdown via telnet: localhost failed"
  102. fn_scriptlog "Warning! Attempting graceful shutdown failed using localhost"
  103. sleep 5
  104. echo -en "\n"
  105. fn_printdots "Attempting graceful shutdown via telnet: using ${telnetip}"
  106. fn_scriptlog "Attempting graceful shutdown via telnet using ${telnetip}"
  107. sdtd_telnet
  108. sleep 1
  109. fi
  110. refused=$(echo -en "\n ${sdtdshutdown}"| grep "Timeout or EOF")
  111. completed=$(echo -en "\n ${sdtdshutdown}"| grep "Completed.")
  112. if [ -n "${refused}" ]; then
  113. fn_printfail "Attempting graceful shutdown via telnet"
  114. fn_scriptlog "Attempting graceful shutdown failed"
  115. fn_scriptlog "${refused}"
  116. elif [ -n "${completed}" ]; then
  117. fn_printok "Attempting graceful shutdown via telnet"
  118. fn_scriptlog "Attempting graceful shutdown succeeded"
  119. else
  120. fn_printfail "Attempting graceful shutdown via telnet: Unknown error"
  121. fn_scriptlog "Attempting graceful shutdown failed"
  122. fn_scriptlog "Unknown error"
  123. fi
  124. sleep 1
  125. echo -en "\n\n"
  126. echo -en "Telnet output:"
  127. echo -en "\n ${sdtdshutdown}"
  128. echo -en "\n\n"
  129. sleep 1
  130. fn_printdots "${servername}"
  131. fn_scriptlog "${servername}"
  132. sleep 5
  133. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  134. if [ "${pid}" == "0" ]; then
  135. fn_printok "${servername} is already stopped using graceful shutdown"
  136. fn_scriptlog "${servername} is already stopped using graceful shutdown"
  137. else
  138. tmux kill-session -t "${servicename}"
  139. fn_printok "${servername}"
  140. fn_scriptlog "Stopped ${servername}"
  141. fi
  142. else
  143. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  144. if [ "${pid}" == "0" ]; then
  145. fn_printfail "${servername} is already stopped"
  146. fn_scriptlog "${servername} is already stopped"
  147. else
  148. if [ "${engine}" == "source" ]||[ "${engine}" == "goldsource" ]; then
  149. sleep 1
  150. fn_printdots "Attempting graceful shutdown"
  151. fn_scriptlog "Attempting graceful shutdown"
  152. tmux send -t "${servicename}" quit ENTER > /dev/null 2>&1
  153. counter=0
  154. while [ "${pid}" != "0" -a $counter -lt 30 ]; do
  155. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  156. sleep 1
  157. let counter=counter+1
  158. if [ "${counter}" -gt "1" ]; then
  159. fn_printdots "Attempting graceful shutdown: ${counter}"
  160. fi
  161. done
  162. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -Ec "^${servicename}:")
  163. if [ "${pid}" == "0" ]; then
  164. fn_printok "Attempting graceful shutdown"
  165. else
  166. fn_printfail "Attempting graceful shutdown"
  167. fi
  168. fi
  169. tmux kill-session -t "${servicename}" > /dev/null 2>&1
  170. fn_printok "${servername}"
  171. fn_scriptlog "Stopped ${servername}"
  172. fi
  173. fi
  174. # Remove lock file
  175. rm -f "${rootdir}/${lockselfname}"
  176. sleep 1
  177. echo -en "\n"
  178. }
  179. if [ "${gamename}" == "Teamspeak 3" ]; then
  180. fn_stop_teamspeak3
  181. else
  182. fn_stop_tmux
  183. if [ "${gamename}" == "ARK: Survivial Evolved" ]; then
  184. fn_stop_ark
  185. echo -en "\n"
  186. fi
  187. fi