command_start.sh 10 KB

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