command_start.sh 6.7 KB

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