command_start.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. check_status.sh
  47. if [ "${status}" != "0" ]; then
  48. # stop running server (if running) to prevent lingering tmux sessions.
  49. exitbypass=1
  50. command_stop.sh
  51. fi
  52. uid=$(date '+%s' | sha1sum | head -c 8)
  53. echo "${uid}" > "${datadir}/${selfname}.uid"
  54. socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")"
  55. fi
  56. if [ "${shortname}" == "av" ]; then
  57. cd "${systemdir}" || exit
  58. else
  59. cd "${executabledir}" || exit
  60. fi
  61. tmux -L "${socketname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  62. # Create logfile.
  63. touch "${consolelog}"
  64. # tmux compiled from source will return "master", therefore ignore it.
  65. if [ "${tmuxv}" == "master" ]; then
  66. fn_script_log "tmux version: master (user compiled)"
  67. echo -e "tmux version: master (user compiled)" >> "${consolelog}"
  68. if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  69. if [ "${logtimestamp}" == "on" ]; then
  70. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  71. else
  72. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  73. fi
  74. fi
  75. elif [ -n "${tmuxv}" ]; then
  76. # tmux pipe-pane not supported in tmux versions < 1.6.
  77. if [ "${tmuxvdigit}" -lt "16" ]; then
  78. echo -e "Console logging disabled: tmux => 1.6 required
  79. https://linuxgsm.com/tmux-upgrade
  80. Currently installed: $(tmux -V)" > "${consolelog}"
  81. # Console logging disabled: Bug in tmux 1.8 breaks logging.
  82. elif [ "${tmuxvdigit}" -eq "18" ]; then
  83. echo -e "Console logging disabled: Bug in tmux 1.8 breaks logging
  84. https://linuxgsm.com/tmux-upgrade
  85. Currently installed: $(tmux -V)" > "${consolelog}"
  86. # Console logging enable or not set.
  87. elif [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  88. if [ "${logtimestamp}" == "on" ]; then
  89. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  90. else
  91. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  92. fi
  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 in settings" >> "${consolelog}"
  101. fn_script_log_info "Console logging disabled by user"
  102. fi
  103. fn_sleep_time_1
  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_fail "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_fail "Unable to start ${servername}: tmux error:"
  112. echo -e ""
  113. echo -e "Command"
  114. fn_messages_separator
  115. echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
  116. echo -e ""
  117. echo -e "Error"
  118. fn_messages_separator
  119. tee -a "${lgsmlog}" < "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  120. # Detected error https://linuxgsm.com/support
  121. if grep -c "Operation not permitted" "${lgsmlogdir}/.${selfname}-tmux-error.tmp"; then
  122. echo -e ""
  123. echo -e "Fix"
  124. fn_messages_separator
  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. # Remove starting lockfile when command ends.
  148. rm -f "${lockdir:?}/${selfname}-starting.lock"
  149. core_exit.sh
  150. else
  151. # Create start lockfile that exists only when the server is running.
  152. date '+%s' > "${lockdir:?}/${selfname}-started.lock"
  153. echo "${version}" >> "${lockdir}/${selfname}-started.lock"
  154. echo "${port}" >> "${lockdir}/${selfname}-started.lock"
  155. fn_print_ok "${servername}"
  156. fn_script_log_pass "Started ${servername}"
  157. # Create last started Lockfile.
  158. date +%s > "${lockdir}/${selfname}-last-started.lock"
  159. fn_print_ok "${servername}"
  160. fn_script_log_pass "Started ${servername}"
  161. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "START" ]; then
  162. alert="started"
  163. alert.sh
  164. elif [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "RESTART" ]; then
  165. alert="restarted"
  166. alert.sh
  167. fi
  168. fi
  169. rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null
  170. echo -en "\n"
  171. }
  172. # If user ran the start command monitor will become enabled.
  173. if [ "${firstcommandname}" == "START" ] || [ "${firstcommandname}" == "RESTART" ]; then
  174. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  175. fi
  176. fn_print_dots ""
  177. check.sh
  178. # If the server already started dont start again.
  179. if [ "${status}" != "0" ]; then
  180. fn_print_dots "${servername}"
  181. fn_print_info_nl "${servername} is already running"
  182. fn_script_log_error "${servername} is already running"
  183. if [ -z "${exitbypass}" ]; then
  184. # Remove starting lockfile when command ends.
  185. rm -f "${lockdir:?}/${selfname}-starting.lock"
  186. core_exit.sh
  187. fi
  188. fi
  189. fix.sh
  190. info_game.sh
  191. core_logs.sh
  192. # Will check for updates if updateonstart is yes.
  193. if [ "${updateonstart}" == "yes" ] || [ "${updateonstart}" == "1" ] || [ "${updateonstart}" == "on" ]; then
  194. exitbypass=1
  195. unset updateonstart
  196. command_update.sh
  197. fn_firstcommand_reset
  198. fi
  199. fn_print_dots "${servername}"
  200. if [ "${shortname}" == "jk2" ]; then
  201. fn_start_jk2
  202. else
  203. fn_start_tmux
  204. fi
  205. # Remove starting lockfile when command ends.
  206. rm -f "${lockdir:?}/${selfname}-starting.lock"
  207. core_exit.sh