command_start.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  10. addtimestamp="gawk '{ print strftime(\\\"[$logtimestampformat]\\\"), \\\$0 }'"
  11. fn_firstcommand_set
  12. # This will allow the Jedi Knight 2 version to be printed in console on start.
  13. # Used to allow update to detect JK2MV server version.
  14. fn_start_jk2() {
  15. fn_start_tmux
  16. tmux -L "${socketname}" end -t "${sessionname}" version ENTER > /dev/null 2>&1
  17. }
  18. fn_start_tmux() {
  19. # check for tmux size variables.
  20. if [[ "${servercfgtmuxwidth}" =~ ^[0-9]+$ ]]; then
  21. sessionwidth="${servercfgtmuxwidth}"
  22. else
  23. sessionwidth="80"
  24. fi
  25. if [[ "${servercfgtmuxheight}" =~ ^[0-9]+$ ]]; then
  26. sessionheight="${servercfgtmuxheight}"
  27. else
  28. sessionheight="23"
  29. fi
  30. # Log rotation.
  31. fn_script_log_info "Rotating log files"
  32. if [ "${engine}" == "unreal2" ] && [ -f "${gamelog}" ]; then
  33. mv "${gamelog}" "${gamelogdate}"
  34. fi
  35. if [ -f "${lgsmlog}" ]; then
  36. mv "${lgsmlog}" "${lgsmlogdate}"
  37. fi
  38. if [ -f "${consolelog}" ]; then
  39. mv "${consolelog}" "${consolelogdate}"
  40. fi
  41. # Create a starting lockfile that only exists while the start command is running.
  42. date '+%s' > "${lockdir:?}/${selfname}-starting.lock"
  43. fn_reload_startparameters
  44. # Create uid to ensure unique tmux socket name.
  45. if [ ! -f "${datadir}/${selfname}.uid" ]; then
  46. # stop running server (if running) to prevent lingering tmux sessions.
  47. exitbypass=1
  48. command_stop.sh
  49. uid=$(date '+%s' | sha1sum | head -c 8)
  50. echo "${uid}" > "${datadir}/${selfname}.uid"
  51. socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")"
  52. fi
  53. if [ "${shortname}" == "av" ]; then
  54. cd "${systemdir}" || exit
  55. else
  56. cd "${executabledir}" || exit
  57. fi
  58. tmux -L "${socketname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  59. # Create logfile.
  60. touch "${consolelog}"
  61. # tmux compiled from source will return "master", therefore ignore it.
  62. if [ "${tmuxv}" == "master" ]; then
  63. fn_script_log "tmux version: master (user compiled)"
  64. echo -e "tmux version: master (user compiled)" >> "${consolelog}"
  65. if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  66. if [ "${logtimestamp}" == "on" ]; then
  67. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  68. else
  69. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  70. fi
  71. fi
  72. elif [ -n "${tmuxv}" ]; then
  73. # tmux pipe-pane not supported in tmux versions < 1.6.
  74. if [ "${tmuxvdigit}" -lt "16" ]; then
  75. echo -e "Console logging disabled: tmux => 1.6 required
  76. https://linuxgsm.com/tmux-upgrade
  77. Currently installed: $(tmux -V)" > "${consolelog}"
  78. # Console logging disabled: Bug in tmux 1.8 breaks logging.
  79. elif [ "${tmuxvdigit}" -eq "18" ]; then
  80. echo -e "Console logging disabled: Bug in tmux 1.8 breaks logging
  81. https://linuxgsm.com/tmux-upgrade
  82. Currently installed: $(tmux -V)" > "${consolelog}"
  83. # Console logging enable or not set.
  84. elif [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  85. if [ "${logtimestamp}" == "on" ]; then
  86. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  87. else
  88. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  89. fi
  90. fi
  91. else
  92. echo -e "Unable to detect tmux version" >> "${consolelog}"
  93. fn_script_log_warn "Unable to detect tmux version"
  94. fi
  95. # Console logging disabled.
  96. if [ "${consolelogging}" == "off" ]; then
  97. echo -e "Console logging disabled in settings" >> "${consolelog}"
  98. fn_script_log_info "Console logging disabled by user"
  99. fi
  100. fn_sleep_time
  101. # If the server fails to start.
  102. check_status.sh
  103. if [ "${status}" == "0" ]; then
  104. fn_print_fail_nl "Unable to start ${servername}"
  105. fn_script_log_fatal "Unable to start ${servername}"
  106. if [ -s "${lgsmlogdir}/.${selfname}-tmux-error.tmp" ]; then
  107. fn_print_fail_nl "Unable to start ${servername}: tmux error:"
  108. fn_script_log_fatal "Unable to start ${servername}: tmux error:"
  109. echo -e ""
  110. echo -e "Command"
  111. echo -e "================================="
  112. echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
  113. echo -e ""
  114. echo -e "Error"
  115. echo -e "================================="
  116. tee -a "${lgsmlog}" < "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  117. # Detected error https://linuxgsm.com/support
  118. if grep -c "Operation not permitted" "${lgsmlogdir}/.${selfname}-tmux-error.tmp"; then
  119. echo -e ""
  120. echo -e "Fix"
  121. echo -e "================================="
  122. if ! grep "tty:" /etc/group | grep "$(whoami)"; then
  123. echo -e "$(whoami) is not part of the tty group."
  124. fn_script_log_info "$(whoami) is not part of the tty group."
  125. group=$(grep tty /etc/group)
  126. echo -e ""
  127. echo -e " ${group}"
  128. fn_script_log_info "${group}"
  129. echo -e ""
  130. echo -e "Run the following command with root privileges."
  131. echo -e ""
  132. echo -e " usermod -G tty $(whoami)"
  133. echo -e ""
  134. echo -e "https://linuxgsm.com/tmux-op-perm"
  135. fn_script_log_info "https://linuxgsm.com/tmux-op-perm"
  136. else
  137. echo -e "No known fix currently. Please log an issue."
  138. fn_script_log_info "No known fix currently. Please log an issue."
  139. echo -e "https://linuxgsm.com/support"
  140. fn_script_log_info "https://linuxgsm.com/support"
  141. fi
  142. fi
  143. fi
  144. # Remove starting lockfile when command ends.
  145. rm -f "${lockdir:?}/${selfname}-starting.lock"
  146. core_exit.sh
  147. else
  148. # Create start lockfile that exists only when the server is running.
  149. date '+%s' > "${lockdir:?}/${selfname}-started.lock"
  150. echo "${version}" >> "${lockdir}/${selfname}-started.lock"
  151. echo "${port}" >> "${lockdir}/${selfname}-started.lock"
  152. fn_print_ok "${servername}"
  153. fn_script_log_pass "Started ${servername}"
  154. # Create last started Lockfile.
  155. date +%s > "${lockdir}/${selfname}-last-started.lock"
  156. fn_print_ok "${servername}"
  157. fn_script_log_pass "Started ${servername}"
  158. fi
  159. rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null
  160. echo -en "\n"
  161. }
  162. check.sh
  163. # If user ran the start command monitor will become enabled.
  164. if [ "${firstcommandname}" == "START" ]||[ "${firstcommandname}" == "RESTART" ]; then
  165. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  166. fi
  167. # If the server already started dont start again.
  168. if [ "${status}" != "0" ]; then
  169. fn_print_dots "${servername}"
  170. fn_print_info_nl "${servername} is already running"
  171. fn_script_log_error "${servername} is already running"
  172. if [ -z "${exitbypass}" ]; then
  173. # Remove starting lockfile when command ends.
  174. rm -f "${lockdir:?}/${selfname}-starting.lock"
  175. core_exit.sh
  176. fi
  177. fi
  178. fix.sh
  179. info_game.sh
  180. core_logs.sh
  181. # Will check for updates if updateonstart is yes.
  182. if [ "${updateonstart}" == "yes" ] || [ "${updateonstart}" == "1" ] || [ "${updateonstart}" == "on" ]; then
  183. exitbypass=1
  184. unset updateonstart
  185. command_update.sh
  186. fn_firstcommand_reset
  187. fi
  188. fn_print_dots "${servername}"
  189. if [ "${shortname}" == "jk2" ]; then
  190. fn_start_jk2
  191. else
  192. fn_start_tmux
  193. fi
  194. # Remove starting lockfile when command ends.
  195. rm -f "${lockdir:?}/${selfname}-starting.lock"
  196. core_exit.sh