4
0

command_start.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. addtimestamp="gawk '{ print strftime(\\\"[$logtimestampformat]\\\"), \\\$0 }'"
  11. fn_firstcommand_set
  12. fn_start_teamspeak3() {
  13. if [ ! -f "${servercfgfullpath}" ]; then
  14. fn_print_warn_nl "${servercfgfullpath} is missing"
  15. fn_script_log_warn "${servercfgfullpath} is missing"
  16. echo " * Creating blank ${servercfg}"
  17. fn_script_log_info "Creating blank ${servercfg}"
  18. fn_sleep_time
  19. echo " * ${servercfg} can remain blank by default."
  20. fn_script_log_info "${servercfgfullpath} can remain blank by default."
  21. fn_sleep_time
  22. echo " * ${servercfg} is located in ${servercfgfullpath}."
  23. fn_script_log_info "${servercfg} is located in ${servercfgfullpath}."
  24. sleep 5
  25. touch "${servercfgfullpath}"
  26. fi
  27. # Accept license.
  28. if [ ! -f "${executabledir}/.ts3server_license_accepted" ]; then
  29. install_eula.sh
  30. fi
  31. fn_start_tmux
  32. }
  33. # This will allow the Jedi Knight 2 version to be printed in console on start.
  34. # Used to allow update to detect JK2MV server version.
  35. fn_start_jk2() {
  36. fn_start_tmux
  37. tmux -L "${sessionname}" send -t "${sessionname}" version ENTER > /dev/null 2>&1
  38. }
  39. fn_start_tmux() {
  40. if [ "${parmsbypass}" ]; then
  41. startparameters=""
  42. fi
  43. # check for tmux size variables.
  44. if [[ "${servercfgtmuxwidth}" =~ ^[0-9]+$ ]]; then
  45. sessionwidth="${servercfgtmuxwidth}"
  46. else
  47. sessionwidth="80"
  48. fi
  49. if [[ "${servercfgtmuxheight}" =~ ^[0-9]+$ ]]; then
  50. sessionheight="${servercfgtmuxheight}"
  51. else
  52. sessionheight="23"
  53. fi
  54. # Log rotation.
  55. fn_script_log_info "Rotating log files"
  56. if [ "${engine}" == "unreal2" ] && [ -f "${gamelog}" ]; then
  57. mv "${gamelog}" "${gamelogdate}"
  58. fi
  59. if [ -f "${lgsmlog}" ]; then
  60. mv "${lgsmlog}" "${lgsmlogdate}"
  61. fi
  62. if [ -f "${consolelog}" ]; then
  63. mv "${consolelog}" "${consolelogdate}"
  64. fi
  65. # Create lockfile
  66. date '+%s' > "${lockdir}/${selfname}.lock"
  67. echo "${version}" >> "${lockdir}/${selfname}.lock"
  68. echo "${port}" >> "${lockdir}/${selfname}.lock"
  69. fn_reload_startparameters
  70. if [ "${shortname}" == "av" ]; then
  71. cd "${systemdir}" || exit
  72. else
  73. cd "${executabledir}" || exit
  74. fi
  75. tmux -L "${sessionname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  76. # Create logfile.
  77. touch "${consolelog}"
  78. # Create last start lock file
  79. date +%s > "${lockdir}/${selfname}-laststart.lock"
  80. # tmux compiled from source will return "master", therefore ignore it.
  81. if [ "${tmuxv}" == "master" ]; then
  82. fn_script_log "tmux version: master (user compiled)"
  83. echo -e "tmux version: master (user compiled)" >> "${consolelog}"
  84. if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  85. if [ "$logtimestamp" == "on" ]; then
  86. tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  87. else
  88. tmux -L "${sessionname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  89. fi
  90. fi
  91. elif [ -n "${tmuxv}" ]; then
  92. # tmux pipe-pane not supported in tmux versions < 1.6.
  93. if [ "${tmuxvdigit}" -lt "16" ]; then
  94. echo -e "Console logging disabled: tmux => 1.6 required
  95. https://linuxgsm.com/tmux-upgrade
  96. Currently installed: $(tmux -V)" > "${consolelog}"
  97. # Console logging disabled: Bug in tmux 1.8 breaks logging.
  98. elif [ "${tmuxvdigit}" -eq "18" ]; then
  99. echo -e "Console logging disabled: Bug in tmux 1.8 breaks logging
  100. https://linuxgsm.com/tmux-upgrade
  101. Currently installed: $(tmux -V)" > "${consolelog}"
  102. # Console logging enable or not set.
  103. elif [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  104. if [ "$logtimestamp" == "on" ]; then
  105. tmux pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  106. else
  107. tmux pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  108. fi
  109. fi
  110. else
  111. echo -e "Unable to detect tmux version" >> "${consolelog}"
  112. fn_script_log_warn "Unable to detect tmux version"
  113. fi
  114. # Console logging disabled.
  115. if [ "${consolelogging}" == "off" ]; then
  116. echo -e "Console logging disabled by user" >> "${consolelog}"
  117. fn_script_log_info "Console logging disabled by user"
  118. fi
  119. fn_sleep_time
  120. # If the server fails to start.
  121. check_status.sh
  122. if [ "${status}" == "0" ]; then
  123. fn_print_fail_nl "Unable to start ${servername}"
  124. fn_script_log_fatal "Unable to start ${servername}"
  125. if [ -s "${lgsmlogdir}/.${selfname}-tmux-error.tmp" ]; then
  126. fn_print_fail_nl "Unable to start ${servername}: tmux error:"
  127. fn_script_log_fatal "Unable to start ${servername}: tmux error:"
  128. echo -e ""
  129. echo -e "Command"
  130. echo -e "================================="
  131. echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
  132. echo -e ""
  133. echo -e "Error"
  134. echo -e "================================="
  135. tee -a "${lgsmlog}" < "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  136. # Detected error https://linuxgsm.com/support
  137. if grep -c "Operation not permitted" "${lgsmlogdir}/.${selfname}-tmux-error.tmp"; 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 -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null
  169. echo -en "\n"
  170. }
  171. check.sh
  172. # Is the server already started.
  173. # $status comes from check_status.sh, which is run by check.sh for this command
  174. if [ "${status}" != "0" ]; then
  175. fn_print_dots "${servername}"
  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_game.sh
  186. core_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. fn_firstcommand_reset
  193. fi
  194. fn_print_dots "${servername}"
  195. if [ "${shortname}" == "ts3" ]; then
  196. fn_start_teamspeak3
  197. elif [ "${shortname}" == "jk2" ]; then
  198. fn_start_jk2
  199. else
  200. fn_start_tmux
  201. fi
  202. core_exit.sh