command_start.sh 6.3 KB

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