command_start.sh 6.5 KB

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