command_start.sh 6.3 KB

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