command_start.sh 6.6 KB

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