command_start.sh 6.2 KB

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