Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2002, Valve LLC. All rights reserved.
  4. #
  5. # a wrapper script for the main hl dedicated server binary.
  6. # Performs auto-restarting of the server on crash. You can
  7. # extend this to log crashes and more.
  8. #
  9.  
  10. # setup the libraries, local dir first!
  11. export LD_LIBRARY_PATH=".:$LD_LIBRARY_PATH"
  12.  
  13. init() {
  14. # Initialises the various variables
  15. # Set up the defaults
  16. GAME="valve"
  17. DEBUG=""
  18. RESTART="yes"
  19. HL=./hlds_linux
  20. HL_DETECT=1
  21. TIMEOUT=10 # time to wait after a crash (in seconds)
  22. CRASH_DEBUG_MSG="email debug.log to linux@valvesoftware.com"
  23. GDB="gdb" # the gdb binary to run
  24. DEBUG_LOG="debug.log"
  25. PID_FILE=""
  26. STEAM=""
  27. STEAMERR=""
  28. SIGINT_ACTION="quit 0" # exit normally on sig int
  29. NO_TRAP=0
  30. AUTO_UPDATE=""
  31. BETA_VERSION=""
  32. PARAMS=$*
  33.  
  34. # Remove any old default pid files
  35. # Cant do this as they may be still running
  36. #rm -f hlds.*.pid
  37.  
  38. # use the $FORCE environment variable if its set
  39. if test -n "$FORCE" ; then
  40. # Note: command line -binary will override this
  41. HL=$FORCE
  42. HL_DETECT=0
  43. fi
  44.  
  45. while test $# -gt 0; do
  46. case "$1" in
  47. "-game")
  48. GAME="$2"
  49. shift ;;
  50. "-debug")
  51. DEBUG=1
  52. # Ensure that PID_FILE is set
  53. if test -z "$PID_FILE"; then
  54. PID_FILE="hlds.$$.pid"
  55. fi ;;
  56. "-norestart")
  57. RESTART="" ;;
  58. "-pidfile")
  59. PID_FILE="$2"
  60. shift ;;
  61. "-binary")
  62. HL="$2"
  63. HL_DETECT=0
  64. shift ;;
  65. "-timeout")
  66. TIMEOUT="$2"
  67. shift ;;
  68. "-gdb")
  69. GDB="$2"
  70. shift ;;
  71. "-debuglog")
  72. DEBUG_LOG="$2"
  73. shift ;;
  74. "-autoupdate")
  75. AUTO_UPDATE="yes"
  76. STEAM="steamcmd/steamcmd.sh"
  77. RESTART="yes" ;;
  78. "-steamerr")
  79. STEAMERR=1 ;;
  80. "-ignoresigint")
  81. SIGINT_ACTION="" ;;
  82. "-notrap")
  83. NO_TRAP=1 ;;
  84. "-beta")
  85. BETA_VERSION="$2"
  86. shift ;;
  87. "-help")
  88. # quit with syntax
  89. quit 2
  90. ;;
  91. esac
  92. shift
  93. done
  94.  
  95. # Ensure we have a game specified
  96. if test -z "$GAME"; then
  97. echo "Unable to determine game type from command line."
  98. quit 1
  99. elif test ! -d "$GAME"; then
  100. echo "Invalid game type '$GAME' sepecified."
  101. quit 1
  102. fi
  103.  
  104. #if test 0 -eq "$NO_TRAP"; then
  105. # Set up the int handler
  106. # N.B. Dont use SIGINT symbolic value
  107. # as its just INT under ksh
  108. #trap "$SIGINT_ACTION" 2
  109. #fi
  110.  
  111. if test ! -f "$HL"; then
  112. echo "Half-life binary '$HL' not found, exiting"
  113. quit 1
  114. elif test ! -x "$HL"; then
  115. # Could try chmod but dont know what we will be
  116. # chmoding so just fail.
  117. echo "Half-life binary '$HL' not executable, exiting"
  118. quit 1
  119. fi
  120.  
  121. # Setup debugging
  122. if test "$DEBUG" -eq 1; then
  123. #turn on core dumps :) (if possible)
  124. echo "Enabling debug mode"
  125. if test "`ulimit -c`" -eq 0 ; then
  126. ulimit -c 2000
  127. fi
  128. GDB_TEST=`$GDB -v`
  129. if test -z "$GDB_TEST"; then
  130. echo "Please install gdb first."
  131. echo "goto http://www.gnu.org/software/gdb/ "
  132. DEBUG=0 # turn off debugging cause gdb isn't installed
  133. fi
  134. fi
  135.  
  136. PID_IN_PARAMS="`echo $PARAMS | grep -e -pidfile`"
  137.  
  138. if test -z "$PID_IN_PARAMS" && test -n "$PID_FILE"; then
  139. HL_CMD="$HL $PARAMS -pidfile $PID_FILE"
  140. else
  141. HL_CMD="$HL $PARAMS"
  142. fi
  143. }
  144.  
  145. syntax () {
  146. # Prints script syntax
  147.  
  148. echo "Syntax:"
  149. echo "$0 [-game <game>] [-debug] [-norestart] [-pidfile]"
  150. echo " [-binary [hlds_linux]"
  151. echo " [-timeout <number>] [-gdb <gdb>] [-autoupdate]"
  152. echo " [-steamerr] [-ignoresigint] [-beta <version>]"
  153. echo " [-debuglog <logname>]"
  154. echo "Params:"
  155. echo "-game <game> Specifies the <game> to run."
  156. echo "-debug Run debugging on failed servers if possible."
  157. echo "-debuglog <logname> Log debug output to this file."
  158. echo "-norestart Don't attempt to restart failed servers."
  159. echo "-pidfile <pidfile> Use the specified <pidfile> to store the server pid."
  160. echo "-binary <binary> Use the specified binary ( no auto detection )."
  161. echo "-timeout <number> Sleep for <number> seconds before restarting"
  162. echo " a failed server."
  163. echo "-gdb <gdb> Use <dbg> as the debugger of failed servers."
  164. echo "-steamerr Quit on steam update failure."
  165. echo "-beta <version> Make use of a beta version of this server from Steam"
  166. echo "-ignoresigint Ignore signal INT ( prevents CTRL+C quitting"
  167. echo " the script )."
  168. echo "-notrap Don't use trap. This prevents automatic"
  169. echo " removal of old lock files."
  170. echo ""
  171. echo "Note: All parameters specified as passed through to the server"
  172. echo "including any not listed."
  173. }
  174.  
  175. debugcore () {
  176. # Debugs any core file if DEBUG is set and
  177. # the exitcode is none 0
  178.  
  179. exitcode=$1
  180.  
  181. if test $exitcode -ne 0; then
  182. if test -n "$DEBUG" ; then
  183. echo "bt" > debug.cmds;
  184. echo "info locals" >> debug.cmds;
  185. echo "info sharedlibrary" >> debug.cmds
  186. echo "info frame" >> debug.cmds; # works, but gives an error... must be last
  187. echo "----------------------------------------------" >> $DEBUG_LOG
  188. echo "CRASH: `date`" >> $DEBUG_LOG
  189. echo "Start Line: $HL_CMD" >> $DEBUG_LOG
  190.  
  191. # check to see if a core was dumped
  192. if test -f core ; then
  193. CORE="core"
  194. elif test -f core.`cat $PID_FILE`; then
  195. CORE=core.`cat $PID_FILE`
  196. elif test -f "$HL.core" ; then
  197. CORE="$HL.core"
  198. fi
  199.  
  200. if test -n "$CORE"; then
  201. $GDB $HL $CORE -x debug.cmds -batch >> $DEBUG_LOG
  202. fi
  203.  
  204. echo "End of crash report" >> $DEBUG_LOG
  205. echo "----------------------------------------------" >> $DEBUG_LOG
  206. echo $CRASH_DEBUG_MSG
  207. rm debug.cmds
  208. else
  209. echo "Add \"-debug\" to the $0 command line to generate a debug.log to help with solving this problem"
  210. fi
  211. fi
  212. }
  213.  
  214.  
  215. update() {
  216. updatesingle
  217. }
  218.  
  219. updatesingle() {
  220. # Run the steam update
  221. # exits on failure if STEAMERR is set
  222.  
  223. if test -n "$AUTO_UPDATE"; then
  224. if test -f "$STEAM"; then
  225. echo "Updating server using Steam."
  226. CMD="$STEAM +logon anonymous +force_install_dir .. +app_update 90 +quit";
  227. if test -n "$BETA_VERSION"; then
  228. CMD="$CMD -beta $BETA_VERSION";
  229. fi
  230.  
  231. STEAMEXE=steamcmd $CMD
  232. if test $? -ne 0; then
  233. if test -n "$STEAMERR"; then
  234. echo "`date`: Steam Update failed, exiting."
  235. quit 1
  236. else
  237. echo "`date`: Steam Update failed, ignoring."
  238. return 0
  239. fi
  240. fi
  241. else
  242. if test -n "$STEAMERR"; then
  243. echo "Could not locate steam binary:$STEAM, exiting.";
  244. quit 1
  245. else
  246. echo "Could not locate steam binary:$STEAM, ignoring.";
  247. return 0
  248. fi
  249. fi
  250. fi
  251.  
  252. return 1
  253. }
  254.  
  255. run() {
  256. # Runs the steam update and server
  257. # Loops if RESTART is set
  258. # Debugs if server failure is detected
  259. # Note: if RESTART is not set then
  260. # 1. DEBUG is set then the server is NOT exec'd
  261. # 2. DEBUG is not set the the server is exec'd
  262.  
  263. if test -n "$RESTART" ; then
  264. echo "Auto-restarting the server on crash"
  265.  
  266. #loop forever
  267. while true
  268. do
  269. # Update if needed
  270. update
  271.  
  272. # Run the server
  273. $HL_CMD
  274. retval=$?
  275. if test $retval -eq 0 && test -z "$RESTART" ; then
  276. break; # if 0 is returned then just quit
  277. fi
  278.  
  279. debugcore $retval
  280.  
  281. echo "`date`: Server restart in $TIMEOUT seconds"
  282.  
  283. # don't thrash the hard disk if the server dies, wait a little
  284. sleep $TIMEOUT
  285. done # while true
  286. else
  287. # Update if needed
  288. update
  289.  
  290. # Run the server
  291. if test "$DEBUG" -eq 0; then
  292. # debug not requested we can exec
  293. exec $HL_CMD
  294. else
  295. # debug requested we can't exec
  296. $HL_CMD
  297. debugcore $?
  298. fi
  299. fi
  300. }
  301.  
  302. quit() {
  303. # Exits with the give error code, 1
  304. # if none specified.
  305. # exit code 2 also prints syntax
  306. exitcode="$1"
  307.  
  308. # default to failure
  309. if test -z "$exitcode"; then
  310. exitcode=1
  311. fi
  312.  
  313. case "$exitcode" in
  314. 0)
  315. echo "`date`: Server Quit" ;;
  316. 2)
  317. syntax ;;
  318. *)
  319. echo "`date`: Server Failed" ;;
  320. esac
  321.  
  322. # Remove pid file
  323. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  324. # The specified pid file
  325. rm -f $PID_FILE
  326. fi
  327.  
  328. # reset SIGINT and then kill ourselves properly
  329. trap - 2
  330. kill -2 $$
  331. }
  332.  
  333. # Initialise
  334. init $*
  335.  
  336. # Run
  337. run
  338.  
  339. # Quit normally
  340. quit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement