command_start.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/bin/bash
  2. # LinuxGSM command_start.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://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. TERM=screen tmux -L "${socketname}" end -t "${sessionname}" version ENTER > /dev/null 2>&1
  17. }
  18. # Hytale: after the server boots, check whether it logged in. If it didn't (no saved login, or one that
  19. # can't be decrypted, e.g. created on another machine or container), start the login, then show the
  20. # login URL and send it as an alert, so it can be approved without opening the console.
  21. # Prints the server console: the console log, or the tmux pane when console logging is off.
  22. fn_start_hyt_console() {
  23. if [ "${consolelogging}" == "off" ]; then
  24. TERM=screen tmux -L "${socketname}" capture-pane -p -J -S - -t "${sessionname}" 2> /dev/null
  25. else
  26. cat "${consolelog}" 2> /dev/null
  27. fi
  28. }
  29. fn_start_hyt_login() {
  30. hytloginurl=""
  31. local hytloginsent="${hytloginpending}"
  32. for _ in {1..120}; do
  33. hytloginurl=$(fn_start_hyt_console | grep -a -oE "https://[^ ]*device/verify\?user_code=[A-Za-z0-9]+" | tail -1)
  34. if [ -n "${hytloginurl}" ]; then
  35. break
  36. fi
  37. # Logged in with the saved server login: nothing to do.
  38. if fn_start_hyt_console | grep -a -q "Authentication successful"; then
  39. fn_script_log_info "Hytale server login restored from saved credentials"
  40. return
  41. fi
  42. # Booted without a usable login: ask the server to start one.
  43. if [ "${hytloginsent}" != "1" ] && fn_start_hyt_console | grep -a -q "Hytale Server Booted" && fn_start_hyt_console | grep -a -q "No server tokens configured"; then
  44. fn_script_log_info "Hytale server has no usable saved login, starting login"
  45. TERM=screen tmux -L "${socketname}" send-keys -t "${sessionname}" "auth login device" ENTER
  46. hytloginsent="1"
  47. fi
  48. sleep 1
  49. done
  50. echo -e ""
  51. if [ -n "${hytloginurl}" ]; then
  52. echo -e "${bold}${lightyellow}Hytale server login required${default}"
  53. echo -e "Open the URL below in a browser and log in with a Hytale account that owns the game."
  54. echo -e "The code expires in 10 minutes. This is only needed once; the server saves the login."
  55. echo -e "${hytloginurl}"
  56. fn_script_log_info "Hytale server login required: ${hytloginurl}"
  57. alert="hytale-login"
  58. alert.sh
  59. else
  60. fn_print_warn_nl "Hytale server login required, but no login URL was found in the console yet"
  61. echo -e "* Run ./${selfname} send \"/auth login device\" and approve the code shown in the console."
  62. fn_script_log_warn "Hytale server login required, but no login URL was found in the console"
  63. fi
  64. }
  65. fn_start_tmux() {
  66. # check for tmux size variables.
  67. if [[ "${servercfgtmuxwidth}" =~ ^[0-9]+$ ]]; then
  68. sessionwidth="${servercfgtmuxwidth}"
  69. else
  70. sessionwidth="80"
  71. fi
  72. if [[ "${servercfgtmuxheight}" =~ ^[0-9]+$ ]]; then
  73. sessionheight="${servercfgtmuxheight}"
  74. else
  75. sessionheight="23"
  76. fi
  77. # Log rotation.
  78. fn_script_log_info "Rotating log files"
  79. if [ "${engine}" == "unreal2" ] && [ -f "${gamelog}" ]; then
  80. mv "${gamelog}" "${gamelogdate}"
  81. fi
  82. if [ -f "${lgsmlog}" ]; then
  83. mv "${lgsmlog}" "${lgsmlogdate}"
  84. fi
  85. if [ -f "${consolelog}" ]; then
  86. mv "${consolelog}" "${consolelogdate}"
  87. fi
  88. # Create a starting lockfile that only exists while the start command is running.
  89. date '+%s' > "${lockdir:?}/${selfname}-starting.lock"
  90. fn_reload_startparameters
  91. # Hytale: with no saved server login file, have the server start its login (OAuth device code) on boot.
  92. # A saved login that can't be used is handled after boot by fn_start_hyt_login.
  93. if [ "${shortname}" == "hyt" ] && [ ! -f "${systemdir}/auth.enc" ]; then
  94. startparameters="${startparameters} --boot-command 'auth login device'"
  95. hytloginpending="1"
  96. fi
  97. # Create uid to ensure unique tmux socket name.
  98. if [ ! -f "${datadir}/${selfname}.uid" ]; then
  99. check_status.sh
  100. if [ "${status}" != "0" ]; then
  101. # stop running server (if running) to prevent lingering tmux sessions.
  102. exitbypass=1
  103. command_stop.sh
  104. fi
  105. uid=$(date '+%s' | sha1sum | head -c 8)
  106. echo "${uid}" > "${datadir}/${selfname}.uid"
  107. socketname="${sessionname}-$(cat "${datadir}/${selfname}.uid")"
  108. fi
  109. if [ "${shortname}" == "av" ]; then
  110. cd "${systemdir}" || exit
  111. else
  112. cd "${executabledir}" || exit
  113. fi
  114. TERM=screen tmux -L "${socketname}" new-session -d -x "${sessionwidth}" -y "${sessionheight}" -s "${sessionname}" "${preexecutable} ${executable} ${startparameters}" 2> "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  115. # Create logfile.
  116. touch "${consolelog}"
  117. # tmux compiled from source will return "master", therefore ignore it.
  118. if [ "${tmuxversion}" == "master" ]; then
  119. fn_script_log "tmux version: master (user compiled)"
  120. echo -e "tmux version: master (user compiled)" >> "${consolelog}"
  121. fi
  122. # Enable console logging.
  123. if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  124. # timestamp will break mcb update check.
  125. if [ "${logtimestamp}" == "on" ] && [ "${shortname}" != "mcb" ]; then
  126. TERM=screen tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  127. else
  128. TERM=screen tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  129. fi
  130. else
  131. echo -e "Console logging disabled in settings" >> "${consolelog}"
  132. fn_script_log_info "Console logging disabled in settings"
  133. fi
  134. fn_sleep_time_1
  135. # If the server fails to start.
  136. check_status.sh
  137. if [ "${status}" == "0" ]; then
  138. fn_print_fail_nl "Unable to start ${servername}"
  139. fn_script_log_fail "Unable to start ${servername}"
  140. if [ -s "${lgsmlogdir}/.${selfname}-tmux-error.tmp" ]; then
  141. fn_print_fail_nl "Unable to start ${servername}: tmux error:"
  142. fn_script_log_fail "Unable to start ${servername}: tmux error"
  143. echo -e ""
  144. echo -e "Command"
  145. fn_messages_separator
  146. echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
  147. echo -e ""
  148. echo -e "Error"
  149. fn_messages_separator
  150. tee -a "${lgsmlog}" < "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  151. # Detected error https://linuxgsm.com/support
  152. if grep -c "Operation not permitted" "${lgsmlogdir}/.${selfname}-tmux-error.tmp"; then
  153. echo -e ""
  154. echo -e "Fix"
  155. fn_messages_separator
  156. if ! grep "tty:" /etc/group | grep "$(whoami)"; then
  157. echo -e "$(whoami) is not part of the tty group."
  158. fn_script_log_info "$(whoami) is not part of the tty group."
  159. group=$(grep tty /etc/group)
  160. echo -e ""
  161. echo -e " ${group}"
  162. fn_script_log_info "${group}"
  163. echo -e ""
  164. echo -e "Run the following command with root privileges."
  165. echo -e ""
  166. echo -e " usermod -G tty $(whoami)"
  167. echo -e ""
  168. echo -e "${italic}https://linuxgsm.com/tmux-op-perm"
  169. fn_script_log_info "https://linuxgsm.com/tmux-op-perm"
  170. else
  171. echo -e "No known fix currently. Please log an issue."
  172. fn_script_log_info "No known fix currently. Please log an issue."
  173. echo -e "${italic}https://linuxgsm.com/support"
  174. fn_script_log_info "https://linuxgsm.com/support"
  175. fi
  176. fi
  177. fi
  178. # Remove starting lockfile when command ends.
  179. rm -f "${lockdir:?}/${selfname}-starting.lock"
  180. core_exit.sh
  181. else
  182. # Create start lockfile that exists only when the server is running.
  183. date '+%s' > "${lockdir:?}/${selfname}-started.lock"
  184. echo "${version}" >> "${lockdir}/${selfname}-started.lock"
  185. echo "${port}" >> "${lockdir}/${selfname}-started.lock"
  186. fn_print_ok "${servername}"
  187. fn_script_log_pass "Started ${servername}"
  188. # Create last started Lockfile.
  189. date +%s > "${lockdir}/${selfname}-last-started.lock"
  190. fn_print_ok "${servername}"
  191. fn_script_log_pass "Started ${servername}"
  192. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "START" ]; then
  193. alert="started"
  194. alert.sh
  195. elif [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "RESTART" ]; then
  196. alert="restarted"
  197. alert.sh
  198. fi
  199. if [ "${shortname}" == "hyt" ]; then
  200. fn_start_hyt_login
  201. fi
  202. fi
  203. rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null
  204. echo -en "\n"
  205. }
  206. # If user ran the start command monitor will become enabled.
  207. if [ "${firstcommandname}" == "START" ] || [ "${firstcommandname}" == "RESTART" ]; then
  208. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  209. fi
  210. fn_print_dots ""
  211. check.sh
  212. # If the server already started dont start again.
  213. if [ "${status}" != "0" ]; then
  214. fn_print_dots "${servername}"
  215. fn_print_skip_nl "${servername} is already running"
  216. fn_script_log_error "${servername} is already running"
  217. if [ -z "${exitbypass}" ]; then
  218. # Remove starting lockfile when command ends.
  219. rm -f "${lockdir:?}/${selfname}-starting.lock"
  220. core_exit.sh
  221. fi
  222. fi
  223. fix.sh
  224. info_game.sh
  225. core_logs.sh
  226. # Will check for updates if updateonstart is yes.
  227. if [ "${updateonstart}" == "yes" ] || [ "${updateonstart}" == "1" ] || [ "${updateonstart}" == "on" ]; then
  228. exitbypass=1
  229. unset updateonstart
  230. command_update.sh
  231. fn_firstcommand_reset
  232. fi
  233. fn_print_dots "${servername}"
  234. if [ "${shortname}" == "jk2" ]; then
  235. fn_start_jk2
  236. else
  237. fn_start_tmux
  238. fi
  239. # Remove starting lockfile when command ends.
  240. rm -f "${lockdir:?}/${selfname}-starting.lock"
  241. core_exit.sh