dodserver 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. #!/bin/bash
  2. # Day of Defeat
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: http://danielgibbs.co.uk
  6. # Version: 061014
  7. #### Variables ####
  8. # Notification Email
  9. # (on|off)
  10. emailnotification="off"
  11. email="email@example.com"
  12. # Steam login
  13. steamuser="anonymous"
  14. steampass=""
  15. # Start Variables
  16. defaultmap="dod_Anzio"
  17. maxplayers="16"
  18. port="27015"
  19. clientport="27005"
  20. ip="0.0.0.0"
  21. # https://developer.valvesoftware.com/wiki/Command_Line_Options#Command-line_parameters_2
  22. fn_parms(){
  23. parms="-game dod -strictportbind -ip ${ip} -port ${port} +clientport ${clientport} +map ${defaultmap} -maxplayers ${maxplayers}"
  24. }
  25. #### Advanced Variables ####
  26. # Steam
  27. appid="90 -beta beta +app_set_config 90 mod dod"
  28. # Server Details
  29. servicename="dod-server"
  30. gamename="Day of Defeat"
  31. engine="goldsource"
  32. # Directories
  33. rootdir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
  34. selfname="$0"
  35. lockselfname=$(echo ".${servicename}.lock")
  36. filesdir="${rootdir}/serverfiles"
  37. systemdir="${filesdir}/dod"
  38. executabledir="${filesdir}"
  39. executable="./hlds_run"
  40. servercfgdir="${systemdir}"
  41. servercfg="${servicename}.cfg"
  42. servercfgfullpath="${servercfgdir}/${servercfg}"
  43. defaultcfg="${servercfgdir}/server.cfg"
  44. backupdir="backups"
  45. # Server Details
  46. servername=$(grep -s hostname "${servercfgfullpath}"|sed 's/hostname //g'|sed 's/"//g')
  47. rcon=$(grep -s rcon_password "${servercfgfullpath}"|sed 's/rcon_password //g'|sed 's/"//g')
  48. # Logging
  49. logdays="7"
  50. gamelogdir="${systemdir}/logs"
  51. scriptlogdir="${rootdir}/log/script"
  52. consolelogdir="${rootdir}/log/console"
  53. scriptlog="${scriptlogdir}/${servicename}-script.log"
  54. consolelog="${consolelogdir}/${servicename}-console.log"
  55. emaillog="${scriptlogdir}/${servicename}-email.log"
  56. scriptlogdate="${scriptlogdir}/${servicename}-script-$(date '+%d-%m-%Y-%H-%M-%S').log"
  57. consolelogdate="${consolelogdir}/${servicename}-console-$(date '+%d-%m-%Y-%H-%M-%S').log"
  58. ##### Script #####
  59. # Do not edit
  60. # unless you know
  61. # what you are doing
  62. fn_scriptlog(){
  63. echo -e "$(date '+%b %d %H:%M:%S') ${servicename}: ${1}" >> ${scriptlog}
  64. }
  65. # [ FAIL ]
  66. fn_printfail(){
  67. echo -en "\r\033[K[\e[0;31m FAIL \e[0;39m] $@"
  68. }
  69. fn_printfailnl(){
  70. echo -e "\r\033[K[\e[0;31m FAIL \e[0;39m] $@"
  71. }
  72. fn_printok(){
  73. echo -en "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  74. }
  75. # [ OK ]
  76. fn_printoknl(){
  77. echo -e "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  78. }
  79. fn_printinfo(){
  80. echo -en "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  81. }
  82. fn_printinfonl(){
  83. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  84. }
  85. # [ INFO ]
  86. fn_printokinfonl(){
  87. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  88. }
  89. fn_printwarn(){
  90. echo -en "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  91. }
  92. fn_printwarnnl(){
  93. echo -e "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  94. }
  95. # [ .... ]
  96. fn_printdots(){
  97. echo -en "\r\033[K[ .... ] $@"
  98. }
  99. fn_rootcheck(){
  100. if [ `whoami` = "root" ]; then
  101. fn_printfailnl "Script will not run as root!"
  102. exit
  103. fi
  104. }
  105. fn_syscheck(){
  106. if [ ! -e "${systemdir}" ]; then
  107. fn_printfailnl "Cannot access ${systemdir}: No such directory"
  108. exit
  109. fi
  110. }
  111. fn_autoip(){
  112. # Identifies the server interface IP
  113. # If multiple interfaces this will need to be set manually
  114. getip=$(ip -o -4 addr|awk '{print $4 }'|grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|grep -v 127.0.0)
  115. getipwc=$(ip -o -4 addr|awk '{print $4 }'|grep -oe '\([0-9]\{1,3\}\.\?\)\{4\}'|grep -v 127.0.0|wc -l)
  116. if [ "${ip}" == "0.0.0.0" ]||[ "${ip}" == "" ]; then
  117. if [ "${getipwc}" -ge "2" ]; then
  118. fn_printwarn "Multiple active network interfaces.\n\n"
  119. echo -en "Manually specify the IP you want to use within the ${selfname} script.\n"
  120. echo -en "Set ip=\"0.0.0.0\" to one of the following:\n"
  121. echo -en "${getip}\n"
  122. exit
  123. else
  124. ip=${getip}
  125. fi
  126. fi
  127. }
  128. fn_logmanager(){
  129. if [ ! -e "${consolelog}" ]; then
  130. touch "${consolelog}"
  131. fi
  132. # log manager will active if finds logs older than ${logdays}
  133. if [ `find "${scriptlogdir}"/* -mtime +${logdays}|wc -l` -ne "0" ]; then
  134. fn_printdots "Starting log cleaner"
  135. sleep 1
  136. fn_printok "Starting log cleaner"
  137. fn_scriptlog "Starting log cleaner"
  138. sleep 1
  139. echo -en "\n"
  140. fn_printinfo "Removing logs older than ${logdays} days"
  141. fn_scriptlog "Removing logs older than ${logdays} days"
  142. sleep 1
  143. echo -en "\n"
  144. if [ "${engine}" == "unreal2" ]; then
  145. find "${gamelogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  146. fi
  147. find "${scriptlogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  148. find "${consolelogdir}"/* -mtime +${logdays}|tee >> "${scriptlog}"
  149. if [ "${engine}" == "unreal2" ]; then
  150. gamecount=$(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l)
  151. fi
  152. scriptcount=$(find "${scriptlogdir}"/* -mtime +${logdays}|wc -l)
  153. consolecount=$(find "${consolelogdir}"/* -mtime +${logdays}|wc -l)
  154. count=$((${scriptcount} + ${consolecount}))
  155. if [ "${engine}" == "unreal2" ]; then
  156. count=$((${scriptcount} + ${consolecount} + ${gamecount}))
  157. else
  158. count=$((${scriptcount} + ${consolecount}))
  159. fi
  160. if [ "${engine}" == "unreal2" ]; then
  161. find "${gamelogdir}"/* -mtime +${logdays} -exec rm {} \;
  162. fi
  163. find "${scriptlogdir}"/* -mtime +${logdays} -exec rm {} \;
  164. find "${consolelogdir}"/* -mtime +${logdays} -exec rm {} \;
  165. fn_printok "Log cleaner removed ${count} log files"
  166. fn_scriptlog "Log cleaner removed ${count} log files"
  167. sleep 1
  168. echo -en "\n"
  169. fi
  170. }
  171. fn_debugserver(){
  172. fn_rootcheck
  173. fn_syscheck
  174. fn_autoip
  175. fn_distro
  176. fn_uptime
  177. fn_load
  178. fn_parms
  179. echo ""
  180. echo "${gamename} Debug"
  181. echo "============================"
  182. echo ""
  183. echo -e "Distro: ${os}"
  184. echo -e "Arch: ${arch}"
  185. echo -e "Kernel: ${kernel}"
  186. echo -e "Hostname: $HOSTNAME"
  187. echo ""
  188. echo "Start parameters:"
  189. echo ${parms}
  190. echo ""
  191. echo -e "Use for identifying server issues only!"
  192. echo -e "Press CTRL+c to drop out of debug mode"
  193. echo -e "\e[0;31mWARNING!\e[0;39m If ${servicename} is already running it will be stopped"
  194. echo ""
  195. while true; do
  196. read -p "Continue? [y/N]" yn
  197. case $yn in
  198. [Yy]* ) break;;
  199. [Nn]* ) echo Exiting; return 1;;
  200. * ) echo "Please answer yes or no.";;
  201. esac
  202. done
  203. fn_stopserver
  204. fn_printdots "Starting debug mode ${servicename}: ${servername}"
  205. sleep 1
  206. fn_printok "Starting debug mode ${servicename}: ${servername}"
  207. sleep 1
  208. fn_scriptlog "Started debug mode ${servername}"
  209. echo -en "\n"
  210. cd "${executabledir}"
  211. ${executable} ${parms} -debug
  212. }
  213. fn_console(){
  214. fn_rootcheck
  215. fn_syscheck
  216. echo ""
  217. echo "${gamename} Console"
  218. echo "============================"
  219. echo ""
  220. echo "Press \"CTRL+b d\" to exit console"
  221. echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
  222. echo ""
  223. while true; do
  224. read -p "Continue? [y/N]" yn
  225. case $yn in
  226. [Yy]* ) break;;
  227. [Nn]* ) echo Exiting; return 1;;
  228. * ) echo "Please answer yes or no.";;
  229. esac
  230. done
  231. fn_printdots "Starting ${servicename} console"
  232. sleep 1
  233. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  234. if [ ${tmuxwc} -eq 1 ]; then
  235. fn_printoknl "Starting ${servicename} console"
  236. fn_scriptlog "Console accessed"
  237. sleep 1
  238. tmux attach-session -t ${servicename}
  239. else
  240. fn_printfailnl "Starting ${servicename} console: ${servername} not running"
  241. sleep 1
  242. while true; do
  243. read -p "Do you want to start the server? [y/N]" yn
  244. case $yn in
  245. [Yy]* ) fn_startserver; break;;
  246. [Nn]* ) break;;
  247. * ) echo "Please answer yes or no.";;
  248. esac
  249. done
  250. fi
  251. }
  252. fn_backupserver(){
  253. fn_rootcheck
  254. fn_syscheck
  255. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  256. echo ""
  257. echo "${gamename} Backup"
  258. echo "============================"
  259. echo ""
  260. echo "The following backup will be created."
  261. echo ""
  262. echo "${backupdir}/${backupname}.tar.gz"
  263. echo ""
  264. while true; do
  265. read -p "Continue? [y/N]" yn
  266. case $yn in
  267. [Yy]* ) break;;
  268. [Nn]* ) echo Exiting; return 1;;
  269. * ) echo "Please answer yes or no.";;
  270. esac
  271. done
  272. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  273. if [ ${tmuxwc} -eq 1 ]; then
  274. echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
  275. while true; do
  276. read -p "Would you like to stop ${servicename} while running the backup? [y/N]" yn
  277. case $yn in
  278. [Yy]* ) fn_stopserver; break;;
  279. [Nn]* ) break;;
  280. * ) echo "Please answer yes or no.";;
  281. esac
  282. done
  283. fi
  284. fn_printdots "Starting backup ${servicename}: ${servername}"
  285. sleep 1
  286. fn_printok "Starting backup ${servicename}: ${servername}"
  287. fn_scriptlog "Backup started"
  288. sleep 1
  289. echo -en "\n"
  290. cd "${rootdir}"
  291. mkdir -pv "${backupdir}" > /dev/null 2>&1
  292. tar -cvzf "${backupdir}/${backupname}.tar.gz" --exclude "${backupdir}" *
  293. echo -en "\r\033[K${servicename} Backup complete"
  294. fn_scriptlog "Backup complete"
  295. }
  296. fn_distro(){
  297. arch=$(uname -m)
  298. kernel=$(uname -r)
  299. if [ -f /etc/lsb-release ]; then
  300. os=$(lsb_release -s -d)
  301. elif [ -f /etc/debian_version ]; then
  302. os="Debian $(cat /etc/debian_version)"
  303. elif [ -f /etc/redhat-release ]; then
  304. os=$(cat /etc/redhat-release)
  305. else
  306. os="$(uname -s) $(uname -r)"
  307. fi
  308. }
  309. fn_uptime(){
  310. uptime=$(</proc/uptime)
  311. uptime=${uptime%%.*}
  312. minutes=$(( uptime/60%60 ))
  313. hours=$(( uptime/60/60%24 ))
  314. days=$(( uptime/60/60/24 ))
  315. }
  316. fn_load(){
  317. load=$(uptime|awk -F 'load average' '{ print $2 }')
  318. }
  319. fn_emailnotification(){
  320. fn_distro
  321. fn_uptime
  322. fn_load
  323. {
  324. echo -e "========================================\n${servicename} details\n========================================\n"
  325. echo -e "Service: ${servicename}"
  326. echo -e "Server: ${servername}"
  327. echo -e "Game: ${gamename}"
  328. echo -e "Failure reason: ${failurereason}"
  329. echo -e "Action Taken: ${actiontaken}\n"
  330. echo -e "========================================\nServer details\n========================================\n"
  331. echo -e "Date: $(date)"
  332. echo -e "Distro: ${os}"
  333. echo -e "Arch: ${arch}"
  334. echo -e "Kernel: ${kernel}"
  335. echo -e "Hostname: $HOSTNAME"
  336. echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
  337. echo -e "Avg Load${load}\n"
  338. echo -e "========================================\nLogs\n========================================\n"
  339. echo -e "Script log\n===================\n"
  340. }|tee "${scriptlogdir}/${servicename}-email.log" > /dev/null 2>&1
  341. tail -25 "${scriptlog}" >> "${emaillog}"
  342. if [ ! -z "${consolelog}" ]; then
  343. echo -e "\n\nConsole log\n====================\n" >> "${emaillog}"
  344. tail -25 "${consolelog}" >> "${emaillog}"
  345. fi
  346. if [ ! -z "${gamelogdir}" ]; then
  347. echo -e "\n\nServer log\n====================\n" >> "${emaillog}"
  348. tail "${gamelogdir}"/*|grep -v "==>"|sed '/^$/d'|tail -25 >> "${emaillog}"
  349. fi
  350. mail -s "${subject}" ${email} < "${emaillog}"
  351. fn_printinfo "Sent email notification to ${email}"
  352. fn_scriptlog "Sent email notification to ${email}"
  353. sleep 1
  354. echo -en "\n"
  355. }
  356. fn_emailtest(){
  357. fn_rootcheck
  358. fn_syscheck
  359. fn_scriptlog "Emailing test notification"
  360. if [ "${emailnotification}" = "on" ]; then
  361. subject="${servicename} Email Test Notification - Testing ${servername}"
  362. failurereason="Testing ${servicename} email notification"
  363. actiontaken="Sent test email...hello is this thing on?"
  364. fn_emailnotification
  365. else
  366. fn_printfailnl "Email notification not enabled"
  367. fn_scriptlog "Email notification not enabled"
  368. fi
  369. sleep 1
  370. echo -en "\n"
  371. }
  372. fn_serverquery(){
  373. # uses gsquery.py to directly query the server
  374. # detects if the server locks up
  375. if [ -f gsquery.py ]; then
  376. if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
  377. gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
  378. port=$((${gameport} + 1))
  379. elif [ "${engine}" == "spark" ]; then
  380. port=$((${port} + 1))
  381. elif [ "${engine}" == "realvirtuality" ]; then
  382. port=${queryport}
  383. fi
  384. fn_printinfo "Monitoring ${servicename}: Detected gsquery.py"
  385. fn_scriptlog "Detected gsquery.py"
  386. sleep 1
  387. fn_printdots "Monitoring ${servicename}: Querying port: ${ip}:${port}: QUERYING"
  388. fn_scriptlog "Querying port: ${ip}:${port}: QUERYING"
  389. sleep 1
  390. serverquery=$(./gsquery.py -a ${ip} -p ${port} -e ${engine} 2>&1)
  391. exitcode=$?
  392. if [ "${exitcode}" == "1" ]||[ "${exitcode}" == "2" ]||[ "${exitcode}" == "3" ]||[ "${exitcode}" == "4" ]; then
  393. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ${serverquery}"
  394. sleep 1
  395. echo -en "\n"
  396. fn_scriptlog "Querying port: ${ip}:${port}: ${serverquery}"
  397. if [[ -z "${secondquery}" ]]; then
  398. fn_printinfo "Monitoring ${servicename}: Waiting 30 seconds to re-query"
  399. fn_scriptlog "Waiting 30 seconds to re-query"
  400. sleep 30
  401. secondquery=1
  402. fn_serverquery
  403. fi
  404. if [ "${emailnotification}" = "on" ]; then
  405. subject="${servicename} Monitor - Starting ${servername}"
  406. failurereason="Failed to query ${servicename}: ${serverquery}"
  407. actiontaken="restarted ${servicename}"
  408. fn_emailnotification
  409. fi
  410. fn_restartserver
  411. exit
  412. elif [ "${exitcode}" == "0" ]; then
  413. fn_printok "Monitoring ${servicename}: Querying port: ${ip}:${port}: OK"
  414. fn_scriptlog "Querying port: ${ip}:${port}: OK"
  415. sleep 1
  416. echo -en "\n"
  417. exit
  418. elif [ "${exitcode}" == "126" ]; then
  419. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ERROR: ./gsquery.py: Permission denied"
  420. fn_scriptlog "Querying port: ${ip}:${port}: ./gsquery.py: Permission denied"
  421. sleep 1
  422. echo -en "\n"
  423. echo "Attempting to resolve automatically"
  424. chmod +x -v gsquery.py
  425. exitcode=$?
  426. if [ "${exitcode}" == "0" ]; then
  427. fn_serverquery
  428. else
  429. echo -en "\nUnable to resolve automatically. Please manually fix permissions\n"
  430. owner=$(ls -al gsquery.py|awk '{ print $3 }')
  431. echo "As user ${owner} or root run the following command"
  432. whoami=$(whoami)
  433. echo -en "\nchown ${whoami}:${whoami} gsquery.py\n\n"
  434. exit
  435. fi
  436. else
  437. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: UNKNOWN ERROR"
  438. fn_scriptlog "Querying port: ${ip}:${port}: UNKNOWN ERROR"
  439. sleep 1
  440. echo -en "\n"
  441. ./gsquery.py -a ${ip} -p ${port} -e ${engine}
  442. exit
  443. fi
  444. fi
  445. }
  446. fn_monitorserver(){
  447. fn_rootcheck
  448. fn_syscheck
  449. fn_autoip
  450. fn_printdots "Monitoring ${servicename}: ${servername}"
  451. fn_scriptlog "Monitoring ${servername}"
  452. sleep 1
  453. if [ ! -f ${lockselfname} ]; then
  454. fn_printinfo "Monitoring ${servicename}: Monitor disabled: No lock file found"
  455. fn_scriptlog "Monitor disabled: No lock file found"
  456. sleep 1
  457. echo -en "\n"
  458. echo "To enable monitor run ${selfname} start"
  459. exit
  460. fi
  461. updatecheck=$(ps -ef|grep "${selfname} update"|grep -v grep|wc -l)
  462. if [ "${updatecheck}" = "0" ]; then
  463. fn_printdots "Monitoring ${servicename}: Checking session: CHECKING"
  464. fn_scriptlog "Checking session: CHECKING"
  465. sleep 1
  466. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  467. if [ ${tmuxwc} -eq 1 ]; then
  468. fn_printok "Monitoring ${servicename}: Checking session: OK"
  469. fn_scriptlog "Checking session: OK"
  470. sleep 1
  471. echo -en "\n"
  472. fn_serverquery
  473. exit
  474. else
  475. fn_printfail "Monitoring ${servicename}: Checking session: FAIL"
  476. fn_scriptlog "Checking session: FAIL"
  477. sleep 1
  478. echo -en "\n"
  479. if [ "${emailnotification}" = "on" ]; then
  480. subject="${servicename} Monitor - Starting ${servername}"
  481. failurereason="${servicename} process not running"
  482. actiontaken="${servicename} has been restarted"
  483. fn_emailnotification
  484. fi
  485. fn_scriptlog "Monitor is starting ${servername}"
  486. fn_startserver
  487. fi
  488. else
  489. fn_printinfonl "Monitoring ${servicename}: Detected SteamCMD is checking for updates"
  490. fn_scriptlog "Detected SteamCMD is checking for updates"
  491. sleep 1
  492. fn_printinfonl "Monitoring ${servicename}: When updates complete ${servicename} will start"
  493. fn_scriptlog "When updates complete ${servicename} will start"
  494. sleep 1
  495. fi
  496. }
  497. fn_updateserver(){
  498. fn_rootcheck
  499. fn_syscheck
  500. fn_printdots "Updating ${servicename}: ${servername}"
  501. sleep 1
  502. fn_printok "Updating ${servicename}: ${servername}"
  503. sleep 1
  504. fn_scriptlog "Updating ${servername}"
  505. cd "${rootdir}"
  506. cd "steamcmd"
  507. ./steamcmd.sh +login ${steamuser} "${steampass}" +force_install_dir "${filesdir}" +app_update ${appid} +quit|tee -a "${scriptlog}"
  508. }
  509. fn_validateserver(){
  510. fn_rootcheck
  511. fn_syscheck
  512. fn_printwarn "Validating may overwrite some customised files."
  513. sleep 1
  514. echo -en "\n"
  515. echo -en "https://developer.valvesoftware.com/wiki/SteamCMD#Validate"
  516. sleep 5
  517. echo -en "\n"
  518. fn_printdots "Validating ${servicename}: ${servername}"
  519. sleep 1
  520. fn_printok "Validating ${servicename}: ${servername}"
  521. sleep 1
  522. fn_scriptlog "Validating ${servername}"
  523. cd "${rootdir}"
  524. cd "steamcmd"
  525. ./steamcmd.sh +login ${steamuser} "${steampass}" +force_install_dir "${filesdir}" +app_update ${appid} validate +quit|tee -a "${scriptlog}"
  526. }
  527. fn_restartserver(){
  528. fn_scriptlog "Restarting ${servername}"
  529. fn_stopserver
  530. fn_startserver
  531. }
  532. fn_stopserver(){
  533. fn_rootcheck
  534. fn_syscheck
  535. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  536. fn_printdots "Stopping ${servicename}: ${servername}"
  537. fn_scriptlog "Stopping ${servername}"
  538. sleep 1
  539. if [ "${pid}" == "0" ]; then
  540. fn_printfail "Stopping ${servicename}: ${servername} is already stopped"
  541. fn_scriptlog "${servername} is already stopped"
  542. else
  543. tmux kill-session -t ${servicename}
  544. fn_printok "Stopping ${servicename}: ${servername}"
  545. fn_scriptlog "Stopped ${servername}"
  546. fi
  547. # Remove lock file
  548. rm -f ${lockselfname}
  549. sleep 1
  550. echo -en "\n"
  551. }
  552. fn_startserver(){
  553. fn_rootcheck
  554. fn_syscheck
  555. fn_autoip
  556. fn_parms
  557. fn_logmanager
  558. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  559. if [ ${tmuxwc} -eq 0 ]; then
  560. fn_scriptlog "Rotating log files"
  561. if [ "${engine}" == "unreal2" ]; then
  562. mv "${gamelog}" "${gamelogdate}"
  563. fi
  564. mv "${scriptlog}" "${scriptlogdate}"
  565. mv "${consolelog}" "${consolelogdate}"
  566. fi
  567. fn_printdots "Starting ${servicename}: ${servername}"
  568. fn_scriptlog "Starting ${servername}"
  569. sleep 1
  570. if [ ${tmuxwc} -eq 1 ]; then
  571. fn_printinfo "Starting ${servicename}: ${servername} is already running"
  572. fn_scriptlog "${servername} is already running"
  573. sleep 1
  574. echo -en "\n"
  575. exit
  576. fi
  577. # Create lock file
  578. date > "${rootdir}/${lockselfname}"
  579. cd "${executabledir}"
  580. tmux new-session -d -s ${servicename} "${executable} ${parms}" 2> ${scriptlogdir}/.${servicename}-tmux-error.tmp
  581. tmux pipe-pane -o -t ${servicename} "exec cat >> '${consolelog}'"
  582. sleep 1
  583. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  584. if [ ${tmuxwc} -eq 0 ]; then
  585. fn_printfailnl "Starting ${servicename}: Failed to start ${servername}"
  586. echo -en " Check log files: ${rootdir}/log"
  587. fn_scriptlog "failed to start ${servername}"
  588. if [ -a ${scriptlogdir}/.${servicename}-tmux-error.tmp ]; then
  589. fn_scriptlog "tmux returned the following error"
  590. cat ${scriptlogdir}/.${servicename}-tmux-error.tmp >> ${scriptlog}
  591. rm ${scriptlogdir}/.${servicename}-tmux-error.tmp
  592. fi
  593. else
  594. fn_printok "Starting ${servicename}: ${servername}"
  595. fn_scriptlog "Started ${servername}"
  596. fi
  597. sleep 1
  598. echo -en "\n"
  599. }
  600. fn_details(){
  601. fn_autoip
  602. servername=$(grep -s hostname "${servercfgfullpath}"|sed 's/hostname //g'|sed 's/"//g')
  603. rcon=$(grep -s rcon_password "${servercfgfullpath}"|sed 's/rcon_password //g'|sed 's/"//g')
  604. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  605. echo ""
  606. echo "${gamename} Server Details"
  607. echo "============================"
  608. echo "Server name: ${servername}"
  609. echo "Server IP: ${ip}:${port}"
  610. echo "RCON password: ${rcon}"
  611. echo "Config file: ${servercfgfullpath}"
  612. echo ""
  613. echo "${servername} Ports"
  614. echo "============================"
  615. echo "Ports the server is currently using."
  616. echo ""
  617. echo "DIRECTION DESCRIPTION PORT"
  618. echo "INBOUND Game/RCON port ${port}"
  619. if [ ! -z ${sourcetvport} ]; then
  620. echo "INBOUND SourceTV port ${sourcetvport}"
  621. fi
  622. echo "OUTBOUND Client port ${clientport}"
  623. echo ""
  624. echo "You can change ports by editing the"
  625. echo "start parameters in ${selfname}."
  626. echo ""
  627. if [ "${pid}" == "0" ]; then
  628. echo -e "Status:\e[0;31m OFFLINE\e[0;39m"
  629. else
  630. echo -e "Status:\e[0;32m ONLINE\e[0;39m"
  631. fi
  632. echo ""
  633. }
  634. #
  635. ## Installer
  636. #
  637. fn_header(){
  638. clear
  639. echo "================================="
  640. echo "${gamename}"
  641. echo "Linux Game Server Manager"
  642. echo "by Daniel Gibbs"
  643. echo "http://danielgibbs.co.uk"
  644. echo "================================="
  645. echo ""
  646. }
  647. fn_steamdl(){
  648. echo "Installing SteamCMD"
  649. echo "================================="
  650. cd "${rootdir}"
  651. mkdir -pv "steamcmd"
  652. sleep 1
  653. cd "steamcmd"
  654. if [ ! -f steamcmd.sh ]; then
  655. wget -nv -N http://media.steampowered.com/client/steamcmd_linux.tar.gz
  656. tar --verbose -zxf steamcmd_linux.tar.gz
  657. rm -v steamcmd_linux.tar.gz
  658. chmod +x steamcmd.sh
  659. sleep 1
  660. else
  661. echo ""
  662. echo "Steam already installed!"
  663. fi
  664. sleep 1
  665. echo ""
  666. }
  667. fn_steaminstall(){
  668. echo "Installing ${gamename} Server"
  669. echo "================================="
  670. sleep 1
  671. mkdir -pv "${filesdir}"
  672. cd "${rootdir}/steamcmd"
  673. STEAMEXE=steamcmd ./steamcmd.sh +login ${steamuser} "${steampass}" +force_install_dir "${filesdir}" +app_update ${appid} validate +quit
  674. echo ""
  675. echo "================================="
  676. while true; do
  677. read -p "Was the install successful? [y/N]" yn
  678. case $yn in
  679. [Yy]* ) break;;
  680. [Nn]* ) fn_retryinstall;;
  681. * ) echo "Please answer yes or no.";;
  682. esac
  683. done
  684. echo ""
  685. }
  686. fn_steamfix(){
  687. echo "Applying steamclient.so fix"
  688. echo "================================="
  689. sleep 1
  690. mkdir -pv "${HOME}/.steam"
  691. mkdir -pv "${HOME}/.steam/sdk32"
  692. cp -v "${rootdir}/steamcmd/linux32/steamclient.so" "${HOME}/.steam/sdk32/steamclient.so"
  693. sleep 1
  694. echo ""
  695. }
  696. fn_loginstall(){
  697. echo "Creating log directorys"
  698. echo "================================="
  699. sleep 1
  700. mkdir -pv "${rootdir}/log"
  701. mkdir -pv "${scriptlogdir}"
  702. touch "${scriptlog}"
  703. mkdir -pv "${consolelogdir}"
  704. touch "${consolelog}"
  705. if [ ! -h ${rootdir}/log/server ]; then
  706. ln -sv "${gamelogdir}" "${rootdir}/log/server"
  707. else
  708. echo "Symbolic link ${gamelogdir} => ${rootdir}/log/server already exists!"
  709. fi
  710. sleep 1
  711. echo ""
  712. }
  713. fn_getquery(){
  714. echo "GameServerQuery"
  715. echo "============================"
  716. while true; do
  717. read -p "Do you want to install GameServerQuery? [y/N]" yn
  718. case $yn in
  719. [Yy]* ) cd "${rootdir}"; wget -nv -N "http://danielgibbs.co.uk/dl/gsquery.py"; chmod +x gsquery.py; break;;
  720. [Nn]* ) echo -e "Not installing GameServerQuery.";break;;
  721. * ) echo "Please answer yes or no.";;
  722. esac
  723. done
  724. echo ""
  725. }
  726. fn_retryinstall(){
  727. while true; do
  728. read -p "Retry install? [y/N]" yn
  729. case $yn in
  730. [Yy]* ) fn_install; exit;;
  731. [Nn]* ) echo Exiting; exit;;
  732. * ) echo "Please answer yes or no.";;
  733. esac
  734. done
  735. }
  736. fn_install(){
  737. fn_rootcheck
  738. fn_header
  739. if [ -d "${filesdir}" ]; then
  740. echo "${gamename} Server is already installed here:"
  741. pwd
  742. echo ""
  743. while true; do
  744. read -p "Continue [y/N]" yn
  745. case $yn in
  746. [Yy]* ) fn_header; break;;
  747. [Nn]* ) echo Exiting; return 1;;
  748. * ) echo "Please answer yes or no.";;
  749. esac
  750. done
  751. fi
  752. echo "Install Directory:"
  753. pwd
  754. echo ""
  755. while true; do
  756. read -p "Continue [y/N]" yn
  757. case $yn in
  758. [Yy]* ) break;;
  759. [Nn]* ) echo Exiting; return 1;;
  760. * ) echo "Please answer yes or no.";;
  761. esac
  762. done
  763. fn_header
  764. fn_steamdl
  765. fn_steaminstall
  766. fn_steaminstall
  767. fn_steaminstall
  768. fn_steaminstall
  769. fn_steamfix
  770. fn_loginstall
  771. fn_getquery
  772. echo "Configuring ${gamename} Server"
  773. echo "================================="
  774. sleep 1
  775. read -p "Enter server name: " servername
  776. read -p "Enter rcon password: " rconpass
  777. sleep 1
  778. echo "Creating server.cfg."
  779. touch "${defaultcfg}"
  780. echo "exec ${servicename}.cfg" > "${defaultcfg}"
  781. sleep 1
  782. echo "Creating ${servicename}.cfg config file."
  783. touch "${servercfgfullpath}"
  784. touch "${systemdir}/listip.cfg"
  785. touch "${systemdir}/banned.cfg"
  786. {
  787. echo -e "// Use this file to configure your DEDICATED server."
  788. echo -e "// This config file is executed on server start."
  789. echo -e ""
  790. echo -e "// disable autoaim"
  791. echo -e "sv_aim 0"
  792. echo -e ""
  793. echo -e "// disable clients' ability to pause the server"
  794. echo -e "pausable 0"
  795. echo -e ""
  796. echo -e "// default server name. Change to \"Bob's Server\", etc."
  797. echo -e "hostname \"${servername}\""
  798. echo -e ""
  799. echo -e "// maximum client movement speed "
  800. echo -e "sv_maxspeed 320"
  801. echo -e ""
  802. echo -e "// 20 minute timelimit"
  803. echo -e "mp_timelimit 20"
  804. echo -e ""
  805. echo -e "// cheats off"
  806. echo -e "sv_cheats 0"
  807. echo -e ""
  808. echo -e "// load ban files"
  809. echo -e "exec listip.cfg"
  810. echo -e "exec banned.cfg"
  811. echo -e ""
  812. echo -e "// rcon passsword"
  813. echo -e "rcon_password \"${rconpass}\""
  814. echo -e ""
  815. echo -e "// Server password"
  816. echo -e "sv_password \"\""
  817. echo -e ""
  818. echo -e "// server logging"
  819. echo -e "log on"
  820. echo -e "sv_logbans 1"
  821. echo -e "sv_logecho 1"
  822. echo -e "sv_logfile 1"
  823. echo -e "sv_log_onefile 0"
  824. }|tee "${servercfgfullpath}" > /dev/null 2>&1
  825. sleep 1
  826. echo ""
  827. fn_header
  828. sleep 1
  829. fn_details
  830. sleep 1
  831. echo "================================="
  832. echo "Install Complete!"
  833. echo ""
  834. echo "To start server type:"
  835. echo "${selfname} start"
  836. echo ""
  837. }
  838. case "$1" in
  839. start)
  840. fn_startserver;;
  841. stop)
  842. fn_stopserver;;
  843. restart)
  844. fn_restartserver;;
  845. update)
  846. fn_updateserver;;
  847. update-restart)
  848. fn_stopserver
  849. fn_updateserver
  850. fn_startserver;;
  851. validate)
  852. fn_validateserver;;
  853. validate-restart)
  854. fn_stopserver
  855. fn_validateserver
  856. fn_startserver;;
  857. monitor)
  858. fn_monitorserver;;
  859. email-test)
  860. fn_emailtest;;
  861. details)
  862. fn_details;;
  863. backup)
  864. fn_backupserver;;
  865. console)
  866. fn_console;;
  867. debug)
  868. fn_debugserver;;
  869. install)
  870. fn_install;;
  871. *)
  872. echo "Usage: $0 {start|stop|restart|update|update-restart|validate|validate-restart|monitor|email-test|details|backup|console|debug|install}"
  873. exit 1;;
  874. esac
  875. exit