4
0

command_start.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. 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 [ "${tmuxversion}" == "master" ]; then
  66. fn_script_log "tmux version: master (user compiled)"
  67. echo -e "tmux version: master (user compiled)" >> "${consolelog}"
  68. fi
  69. # Enable console logging.
  70. if [ "${consolelogging}" == "on" ] || [ -z "${consolelogging}" ]; then
  71. # timestamp will break mcb update check.
  72. if [ "${logtimestamp}" == "on" ] && [ "${shortname}" != "mcb" ]; then
  73. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec bash -c \"cat | $addtimestamp\" >> '${consolelog}'"
  74. else
  75. tmux -L "${socketname}" pipe-pane -o -t "${sessionname}" "exec cat >> '${consolelog}'"
  76. fi
  77. else
  78. echo -e "Console logging disabled in settings" >> "${consolelog}"
  79. fn_script_log_info "Console logging disabled in settings"
  80. fi
  81. fn_sleep_time_1
  82. # If the server fails to start.
  83. check_status.sh
  84. if [ "${status}" == "0" ]; then
  85. fn_print_fail_nl "Unable to start ${servername}"
  86. fn_script_log_fail "Unable to start ${servername}"
  87. if [ -s "${lgsmlogdir}/.${selfname}-tmux-error.tmp" ]; then
  88. fn_print_fail_nl "Unable to start ${servername}: tmux error:"
  89. fn_script_log_fail "Unable to start ${servername}: tmux error"
  90. echo -e ""
  91. echo -e "Command"
  92. fn_messages_separator
  93. echo -e "tmux -L \"${sessionname}\" new-session -d -s \"${sessionname}\" \"${preexecutable} ${executable} ${startparameters}\"" | tee -a "${lgsmlog}"
  94. echo -e ""
  95. echo -e "Error"
  96. fn_messages_separator
  97. tee -a "${lgsmlog}" < "${lgsmlogdir}/.${selfname}-tmux-error.tmp"
  98. # Detected error https://linuxgsm.com/support
  99. if grep -c "Operation not permitted" "${lgsmlogdir}/.${selfname}-tmux-error.tmp"; then
  100. echo -e ""
  101. echo -e "Fix"
  102. fn_messages_separator
  103. if ! grep "tty:" /etc/group | grep "$(whoami)"; then
  104. echo -e "$(whoami) is not part of the tty group."
  105. fn_script_log_info "$(whoami) is not part of the tty group."
  106. group=$(grep tty /etc/group)
  107. echo -e ""
  108. echo -e " ${group}"
  109. fn_script_log_info "${group}"
  110. echo -e ""
  111. echo -e "Run the following command with root privileges."
  112. echo -e ""
  113. echo -e " usermod -G tty $(whoami)"
  114. echo -e ""
  115. echo -e "${italic}https://linuxgsm.com/tmux-op-perm"
  116. fn_script_log_info "https://linuxgsm.com/tmux-op-perm"
  117. else
  118. echo -e "No known fix currently. Please log an issue."
  119. fn_script_log_info "No known fix currently. Please log an issue."
  120. echo -e "${italic}https://linuxgsm.com/support"
  121. fn_script_log_info "https://linuxgsm.com/support"
  122. fi
  123. fi
  124. fi
  125. # Remove starting lockfile when command ends.
  126. rm -f "${lockdir:?}/${selfname}-starting.lock"
  127. core_exit.sh
  128. else
  129. # Create start lockfile that exists only when the server is running.
  130. date '+%s' > "${lockdir:?}/${selfname}-started.lock"
  131. echo "${version}" >> "${lockdir}/${selfname}-started.lock"
  132. echo "${port}" >> "${lockdir}/${selfname}-started.lock"
  133. fn_print_ok "${servername}"
  134. fn_script_log_pass "Started ${servername}"
  135. # Create last started Lockfile.
  136. date +%s > "${lockdir}/${selfname}-last-started.lock"
  137. fn_print_ok "${servername}"
  138. fn_script_log_pass "Started ${servername}"
  139. if [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "START" ]; then
  140. alert="started"
  141. alert.sh
  142. elif [ "${statusalert}" == "on" ] && [ "${firstcommandname}" == "RESTART" ]; then
  143. alert="restarted"
  144. alert.sh
  145. fi
  146. fi
  147. rm -f "${lgsmlogdir:?}/.${selfname}-tmux-error.tmp" 2> /dev/null
  148. echo -en "\n"
  149. }
  150. # If user ran the start command monitor will become enabled.
  151. if [ "${firstcommandname}" == "START" ] || [ "${firstcommandname}" == "RESTART" ]; then
  152. date '+%s' > "${lockdir:?}/${selfname}-monitoring.lock"
  153. fi
  154. fn_print_dots ""
  155. check.sh
  156. # If the server already started dont start again.
  157. if [ "${status}" != "0" ]; then
  158. fn_print_dots "${servername}"
  159. fn_print_skip_nl "${servername} is already running"
  160. fn_script_log_error "${servername} is already running"
  161. if [ -z "${exitbypass}" ]; then
  162. # Remove starting lockfile when command ends.
  163. rm -f "${lockdir:?}/${selfname}-starting.lock"
  164. core_exit.sh
  165. fi
  166. fi
  167. fix.sh
  168. info_game.sh
  169. core_logs.sh
  170. # Will check for updates if updateonstart is yes.
  171. if [ "${updateonstart}" == "yes" ] || [ "${updateonstart}" == "1" ] || [ "${updateonstart}" == "on" ]; then
  172. exitbypass=1
  173. unset updateonstart
  174. command_update.sh
  175. fn_firstcommand_reset
  176. fi
  177. fn_print_dots "${servername}"
  178. if [ "${shortname}" == "jk2" ]; then
  179. fn_start_jk2
  180. else
  181. fn_start_tmux
  182. fi
  183. # Remove starting lockfile when command ends.
  184. rm -f "${lockdir:?}/${selfname}-starting.lock"
  185. core_exit.sh