command_start.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/bin/bash
  2. # LinuxGSM command_start.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Starts the server.
  7. local commandname="START"
  8. local commandaction="Starting"
  9. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. fn_start_teamspeak3(){
  11. if [ ! -e "${servercfgfullpath}" ]; then
  12. fn_print_warn_nl "${servercfgfullpath} is missing"
  13. fn_script_log_warn "${servercfgfullpath} is missing"
  14. echo " * Creating blank ${servercfg}"
  15. fn_script_log_info "Creating blank ${servercfg}"
  16. sleep 1
  17. echo " * ${servercfg} can remain blank by default."
  18. fn_script_log_info "${servercfgfullpath} can remain blank by default."
  19. sleep 1
  20. echo " * ${servercfg} is located in ${servercfgfullpath}."
  21. fn_script_log_info "${servercfg} is located in ${servercfgfullpath}."
  22. sleep 5
  23. touch "${servercfgfullpath}"
  24. fi
  25. sleep 0.5
  26. if [ -f "${lgsmlog}" ]; then
  27. mv "${lgsmlog}" "${lgsmlogdate}"
  28. fi
  29. # Create lockfile
  30. date > "${rootdir}/${lockselfname}"
  31. # Accept license
  32. if [ ! -f "${executabledir}/.ts3server_license_accepted" ]; then
  33. fn_script_log "Accepting ts3server license: ${executabledir}/LICENSE"
  34. fn_print_info_nl "Accepting TeamSpeak license:"
  35. echo " * ${executabledir}/LICENSE"
  36. sleep 3
  37. touch "${executabledir}/.ts3server_license_accepted"
  38. fi
  39. cd "${executabledir}"
  40. if [ "${ts3serverpass}" == "1" ]; then
  41. ./ts3server_startscript.sh start serveradmin_password="${newpassword}" inifile="${servercfgfullpath}" > /dev/null 2>&1
  42. else
  43. ./ts3server_startscript.sh start inifile="${servercfgfullpath}" > /dev/null 2>&1
  44. fi
  45. sleep 0.5
  46. check_status.sh
  47. if [ "${status}" == "0" ]; then
  48. fn_print_fail_nl "Unable to start ${servername}"
  49. fn_script_log_fatal "Unable to start ${servername}"
  50. echo -e " Check log files: ${logdir}"
  51. core_exit.sh
  52. else
  53. fn_print_ok_nl "${servername}"
  54. fn_script_log_pass "Started ${servername}"
  55. fi
  56. }
  57. fn_start_tmux(){
  58. if [ "${parmsbypass}" ]; then
  59. parms=""
  60. else
  61. fn_parms
  62. fi
  63. # check for tmux size variables
  64. if [[ "${servercfgtmuxwidth}" =~ ^[0-9]+$ ]]; then
  65. sessionwidth="${servercfgtmuxwidth}"
  66. else
  67. sessionwidth="80"
  68. fi
  69. if [[ "${servercfgtmuxheight}" =~ ^[0-9]+$ ]]; then
  70. sessionheight="${servercfgtmuxheight}"
  71. else
  72. sessionheight="23"
  73. fi
  74. # Log rotation
  75. fn_script_log_info "Rotating log files"
  76. if [ "${engine}" == "unreal2" ]&&[ -f "${gamelog}" ]; then
  77. mv "${gamelog}" "${gamelogdate}"
  78. fi
  79. if [ -f "${lgsmlog}" ]; then
  80. mv "${lgsmlog}" "${lgsmlogdate}"
  81. fi
  82. if [ -f "${consolelog}" ]; then
  83. mv "${consolelog}" "${consolelogdate}"
  84. fi
  85. # Create lockfile
  86. date > "${rootdir}/${lockselfname}"
  87. cd "${executabledir}"
  88. tmux new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${servicename}" "${executable} ${parms}" 2> "${lgsmlogdir}/.${servicename}-tmux-error.tmp"
  89. # Create logfile
  90. touch "${consolelog}"
  91. # Get tmux version
  92. tmuxversion="$(tmux -V | sed "s/tmux //" | sed -n '1 p')"
  93. # Tmux compiled from source will return "master", therefore ignore it
  94. if [ "$(tmux -V | sed "s/tmux //" | sed -n '1 p')" == "master" ]; then
  95. fn_script_log "Tmux version: master (user compiled)"
  96. echo "Tmux version: master (user compiled)" >> "${consolelog}"
  97. if [ "${consolelogging}" == "on" ]||[ -z "${consolelogging}" ]; then
  98. tmux pipe-pane -o -t "${servicename}" "exec cat >> '${consolelog}'"
  99. fi
  100. elif [ -n "${tmuxversion}" ]; then
  101. # Get the digit version of tmux
  102. tmuxversion="$(tmux -V | sed "s/tmux //" | sed -n '1 p' | tr -cd '[:digit:]')"
  103. # tmux pipe-pane not supported in tmux versions < 1.6
  104. if [ "${tmuxversion}" -lt "16" ]; then
  105. echo "Console logging disabled: Tmux => 1.6 required
  106. https://linuxgsm.com/tmux-upgrade
  107. Currently installed: $(tmux -V)" > "${consolelog}"
  108. # Console logging disabled: Bug in tmux 1.8 breaks logging
  109. elif [ "${tmuxversion}" -eq "18" ]; then
  110. echo "Console logging disabled: Bug in tmux 1.8 breaks logging
  111. https://linuxgsm.com/tmux-upgrade
  112. Currently installed: $(tmux -V)" > "${consolelog}"
  113. # Console logging enable or not set
  114. elif [ "${consolelogging}" == "on" ]||[ -z "${consolelogging}" ]; then
  115. tmux pipe-pane -o -t "${servicename}" "exec cat >> '${consolelog}'"
  116. fi
  117. else
  118. echo "Unable to detect tmux version" >> "${consolelog}"
  119. fn_script_log_warn "Unable to detect tmux version"
  120. fi
  121. # Console logging disabled
  122. if [ "${consolelogging}" == "off" ]; then
  123. echo "Console logging disabled by user" >> "${consolelog}"
  124. fn_script_log_info "Console logging disabled by user"
  125. fi
  126. sleep 0.5
  127. # If the server fails to start
  128. check_status.sh
  129. if [ "${status}" == "0" ]; then
  130. fn_print_fail_nl "Unable to start ${servername}"
  131. fn_script_log_fatal "Unable to start ${servername}"
  132. sleep 0.5
  133. if [ -s "${lgsmlogdir}/.${servicename}-tmux-error.tmp" ]; then
  134. fn_print_fail_nl "Unable to start ${servername}: Tmux error:"
  135. fn_script_log_fatal "Unable to start ${servername}: Tmux error:"
  136. echo ""
  137. echo "Command"
  138. echo "================================="
  139. echo "tmux new-session -d -s \"${servicename}\" \"${executable} ${parms}\"" | tee -a "${lgsmlog}"
  140. echo ""
  141. echo "Error"
  142. echo "================================="
  143. cat "${lgsmlogdir}/.${servicename}-tmux-error.tmp" | tee -a "${lgsmlog}"
  144. # Detected error https://linuxgsm.com/support
  145. if grep -c "Operation not permitted" "${lgsmlogdir}/.${servicename}-tmux-error.tmp"
  146. then
  147. echo ""
  148. echo "Fix"
  149. echo "================================="
  150. if [ ! $(grep "tty:" /etc/group|grep "$(whoami)") ]; then
  151. echo "$(whoami) is not part of the tty group."
  152. fn_script_log_info "$(whoami) is not part of the tty group."
  153. group=$(grep tty /etc/group)
  154. echo ""
  155. echo " ${group}"
  156. fn_script_log_info "${group}"
  157. echo ""
  158. echo "Run the following command with root privileges."
  159. echo ""
  160. echo " usermod -G tty $(whoami)"
  161. echo ""
  162. echo "https://linuxgsm.com/tmux-op-perm"
  163. fn_script_log_info "https://linuxgsm.com/tmux-op-perm"
  164. else
  165. echo "No known fix currently. Please log an issue."
  166. fn_script_log_info "No known fix currently. Please log an issue."
  167. echo "https://linuxgsm.com/support"
  168. fn_script_log_info "https://linuxgsm.com/support"
  169. fi
  170. fi
  171. fi
  172. core_exit.sh
  173. else
  174. fn_print_ok "${servername}"
  175. fn_script_log_pass "Started ${servername}"
  176. fi
  177. rm "${lgsmlogdir}/.${servicename}-tmux-error.tmp"
  178. echo -en "\n"
  179. }
  180. fn_print_dots "${servername}"
  181. sleep 0.5
  182. check.sh
  183. # Is the server already started
  184. if [ "${status}" != "0" ]; then # $status comes from check_status.sh, which is run by check.sh for this command
  185. fn_print_info_nl "${servername} is already running"
  186. fn_script_log_error "${servername} is already running"
  187. if [ -z "${exitbypass}" ]; then
  188. core_exit.sh
  189. fi
  190. fi
  191. if [ -z "${fixbypass}" ];then
  192. fix.sh
  193. fi
  194. info_config.sh
  195. logs.sh
  196. # Will check for updates is updateonstart is yes
  197. if [ "${updateonstart}" == "yes" ]||[ "${updateonstart}" == "1" ]||[ "${updateonstart}" == "on" ]; then
  198. exitbypass=1
  199. unset updateonstart
  200. command_update.sh
  201. fi
  202. if [ "${shortname}" == "ts3" ]; then
  203. fn_start_teamspeak3
  204. else
  205. fn_start_tmux
  206. fi
  207. core_exit.sh