command_start.sh 11 KB

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