dodserver 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. #!/bin/bash
  2. # Day of Defeat
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: http://danielgibbs.co.uk
  6. # Version: 231114
  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. if [ ! -f ${rootdir}/functions/fn_debugserver ]; then
  173. functionfile="fn_debugserver"
  174. fn_fninstall
  175. fi
  176. source ${rootdir}/functions/fn_debugserver
  177. }
  178. fn_console(){
  179. fn_rootcheck
  180. fn_syscheck
  181. echo ""
  182. echo "${gamename} Console"
  183. echo "============================"
  184. echo ""
  185. echo "Press \"CTRL+b d\" to exit console"
  186. echo -e "\e[0;31mWARNING!\e[0;39m Do NOT press CTRL+c to exit"
  187. echo ""
  188. while true; do
  189. read -p "Continue? [y/N]" yn
  190. case $yn in
  191. [Yy]* ) break;;
  192. [Nn]* ) echo Exiting; return 1;;
  193. * ) echo "Please answer yes or no.";;
  194. esac
  195. done
  196. fn_printdots "Starting ${servicename} console"
  197. sleep 1
  198. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  199. if [ ${tmuxwc} -eq 1 ]; then
  200. fn_printoknl "Starting ${servicename} console"
  201. fn_scriptlog "Console accessed"
  202. sleep 1
  203. tmux attach-session -t ${servicename}
  204. else
  205. fn_printfailnl "Starting ${servicename} console: ${servername} not running"
  206. sleep 1
  207. while true; do
  208. read -p "Do you want to start the server? [y/N]" yn
  209. case $yn in
  210. [Yy]* ) fn_startserver; break;;
  211. [Nn]* ) break;;
  212. * ) echo "Please answer yes or no.";;
  213. esac
  214. done
  215. fi
  216. }
  217. fn_backupserver(){
  218. fn_rootcheck
  219. fn_syscheck
  220. backupname="${servicename}-$(date '+%Y-%m-%d-%H%M%S')"
  221. echo ""
  222. echo "${gamename} Backup"
  223. echo "============================"
  224. echo ""
  225. echo "The following backup will be created."
  226. echo ""
  227. echo "${backupdir}/${backupname}.tar.gz"
  228. echo ""
  229. while true; do
  230. read -p "Continue? [y/N]" yn
  231. case $yn in
  232. [Yy]* ) break;;
  233. [Nn]* ) echo Exiting; return 1;;
  234. * ) echo "Please answer yes or no.";;
  235. esac
  236. done
  237. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  238. if [ ${tmuxwc} -eq 1 ]; then
  239. echo -e "\e[0;31mWARNING!\e[0;39m ${servicename} is currently running"
  240. while true; do
  241. read -p "Would you like to stop ${servicename} while running the backup? [y/N]" yn
  242. case $yn in
  243. [Yy]* ) fn_stopserver; break;;
  244. [Nn]* ) break;;
  245. * ) echo "Please answer yes or no.";;
  246. esac
  247. done
  248. fi
  249. fn_printdots "Starting backup ${servicename}: ${servername}"
  250. sleep 1
  251. fn_printok "Starting backup ${servicename}: ${servername}"
  252. fn_scriptlog "Backup started"
  253. sleep 1
  254. echo -en "\n"
  255. cd "${rootdir}"
  256. mkdir -pv "${backupdir}" > /dev/null 2>&1
  257. tar -cvzf "${backupdir}/${backupname}.tar.gz" --exclude "${backupdir}" *
  258. echo -en "\r\033[K${servicename} Backup complete"
  259. fn_scriptlog "Backup complete"
  260. }
  261. fn_distro(){
  262. arch=$(uname -m)
  263. kernel=$(uname -r)
  264. if [ -f /etc/lsb-release ]; then
  265. os=$(lsb_release -s -d)
  266. elif [ -f /etc/debian_version ]; then
  267. os="Debian $(cat /etc/debian_version)"
  268. elif [ -f /etc/redhat-release ]; then
  269. os=$(cat /etc/redhat-release)
  270. else
  271. os="$(uname -s) $(uname -r)"
  272. fi
  273. }
  274. fn_uptime(){
  275. uptime=$(</proc/uptime)
  276. uptime=${uptime%%.*}
  277. minutes=$(( uptime/60%60 ))
  278. hours=$(( uptime/60/60%24 ))
  279. days=$(( uptime/60/60/24 ))
  280. }
  281. fn_load(){
  282. load=$(uptime|awk -F 'load average' '{ print $2 }')
  283. }
  284. fn_emailnotification(){
  285. fn_distro
  286. fn_uptime
  287. fn_load
  288. {
  289. echo -e "========================================\n${servicename} details\n========================================\n"
  290. echo -e "Service: ${servicename}"
  291. echo -e "Server: ${servername}"
  292. echo -e "Game: ${gamename}"
  293. echo -e "Failure reason: ${failurereason}"
  294. echo -e "Action Taken: ${actiontaken}\n"
  295. echo -e "========================================\nServer details\n========================================\n"
  296. echo -e "Date: $(date)"
  297. echo -e "Distro: ${os}"
  298. echo -e "Arch: ${arch}"
  299. echo -e "Kernel: ${kernel}"
  300. echo -e "Hostname: $HOSTNAME"
  301. echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
  302. echo -e "Avg Load${load}\n"
  303. echo -e "========================================\nLogs\n========================================\n"
  304. echo -e "Script log\n===================\n"
  305. }|tee "${scriptlogdir}/${servicename}-email.log" > /dev/null 2>&1
  306. tail -25 "${scriptlog}" >> "${emaillog}"
  307. if [ ! -z "${consolelog}" ]; then
  308. echo -e "\n\nConsole log\n====================\n" >> "${emaillog}"
  309. tail -25 "${consolelog}" >> "${emaillog}"
  310. fi
  311. if [ ! -z "${gamelogdir}" ]; then
  312. echo -e "\n\nServer log\n====================\n" >> "${emaillog}"
  313. tail "${gamelogdir}"/*|grep -v "==>"|sed '/^$/d'|tail -25 >> "${emaillog}"
  314. fi
  315. mail -s "${subject}" ${email} < "${emaillog}"
  316. fn_printinfo "Sent email notification to ${email}"
  317. fn_scriptlog "Sent email notification to ${email}"
  318. sleep 1
  319. echo -en "\n"
  320. }
  321. fn_emailtest(){
  322. fn_rootcheck
  323. fn_syscheck
  324. fn_scriptlog "Emailing test notification"
  325. if [ "${emailnotification}" = "on" ]; then
  326. subject="${servicename} Email Test Notification - Testing ${servername}"
  327. failurereason="Testing ${servicename} email notification"
  328. actiontaken="Sent test email...hello is this thing on?"
  329. fn_emailnotification
  330. else
  331. fn_printfailnl "Email notification not enabled"
  332. fn_scriptlog "Email notification not enabled"
  333. fi
  334. sleep 1
  335. echo -en "\n"
  336. }
  337. fn_serverquery(){
  338. # uses gsquery.py to directly query the server
  339. # detects if the server locks up
  340. if [ -f gsquery.py ]; then
  341. if [ "${engine}" == "unreal" ]||[ "${engine}" == "unreal2" ]; then
  342. gameport=$(grep Port= ${systemdir}/${ini}|grep -v Master|grep -v LAN|grep -v Proxy|grep -v Listen|sed 's/\Port=//g')
  343. port=$((${gameport} + 1))
  344. elif [ "${engine}" == "spark" ]; then
  345. port=$((${port} + 1))
  346. elif [ "${engine}" == "realvirtuality" ]; then
  347. port=${queryport}
  348. fi
  349. fn_printinfo "Monitoring ${servicename}: Detected gsquery.py"
  350. fn_scriptlog "Detected gsquery.py"
  351. sleep 1
  352. fn_printdots "Monitoring ${servicename}: Querying port: ${ip}:${port}: QUERYING"
  353. fn_scriptlog "Querying port: ${ip}:${port}: QUERYING"
  354. sleep 1
  355. serverquery=$(./gsquery.py -a ${ip} -p ${port} -e ${engine} 2>&1)
  356. exitcode=$?
  357. if [ "${exitcode}" == "1" ]||[ "${exitcode}" == "2" ]||[ "${exitcode}" == "3" ]||[ "${exitcode}" == "4" ]; then
  358. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ${serverquery}"
  359. sleep 1
  360. echo -en "\n"
  361. fn_scriptlog "Querying port: ${ip}:${port}: ${serverquery}"
  362. if [[ -z "${secondquery}" ]]; then
  363. fn_printinfo "Monitoring ${servicename}: Waiting 30 seconds to re-query"
  364. fn_scriptlog "Waiting 30 seconds to re-query"
  365. sleep 30
  366. secondquery=1
  367. fn_serverquery
  368. fi
  369. if [ "${emailnotification}" = "on" ]; then
  370. subject="${servicename} Monitor - Starting ${servername}"
  371. failurereason="Failed to query ${servicename}: ${serverquery}"
  372. actiontaken="restarted ${servicename}"
  373. fn_emailnotification
  374. fi
  375. fn_restartserver
  376. exit
  377. elif [ "${exitcode}" == "0" ]; then
  378. fn_printok "Monitoring ${servicename}: Querying port: ${ip}:${port}: OK"
  379. fn_scriptlog "Querying port: ${ip}:${port}: OK"
  380. sleep 1
  381. echo -en "\n"
  382. exit
  383. elif [ "${exitcode}" == "126" ]; then
  384. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: ERROR: ./gsquery.py: Permission denied"
  385. fn_scriptlog "Querying port: ${ip}:${port}: ./gsquery.py: Permission denied"
  386. sleep 1
  387. echo -en "\n"
  388. echo "Attempting to resolve automatically"
  389. chmod +x -v gsquery.py
  390. exitcode=$?
  391. if [ "${exitcode}" == "0" ]; then
  392. fn_serverquery
  393. else
  394. echo -en "\nUnable to resolve automatically. Please manually fix permissions\n"
  395. owner=$(ls -al gsquery.py|awk '{ print $3 }')
  396. echo "As user ${owner} or root run the following command"
  397. whoami=$(whoami)
  398. echo -en "\nchown ${whoami}:${whoami} gsquery.py\n\n"
  399. exit
  400. fi
  401. else
  402. fn_printfail "Monitoring ${servicename}: Querying port: ${ip}:${port}: UNKNOWN ERROR"
  403. fn_scriptlog "Querying port: ${ip}:${port}: UNKNOWN ERROR"
  404. sleep 1
  405. echo -en "\n"
  406. ./gsquery.py -a ${ip} -p ${port} -e ${engine}
  407. exit
  408. fi
  409. fi
  410. }
  411. fn_monitorserver(){
  412. fn_rootcheck
  413. fn_syscheck
  414. fn_autoip
  415. fn_printdots "Monitoring ${servicename}: ${servername}"
  416. fn_scriptlog "Monitoring ${servername}"
  417. sleep 1
  418. if [ ! -f ${lockselfname} ]; then
  419. fn_printinfo "Monitoring ${servicename}: Monitor disabled: No lock file found"
  420. fn_scriptlog "Monitor disabled: No lock file found"
  421. sleep 1
  422. echo -en "\n"
  423. echo "To enable monitor run ${selfname} start"
  424. exit
  425. fi
  426. updatecheck=$(ps -ef|grep "${selfname} update"|grep -v grep|wc -l)
  427. if [ "${updatecheck}" = "0" ]; then
  428. fn_printdots "Monitoring ${servicename}: Checking session: CHECKING"
  429. fn_scriptlog "Checking session: CHECKING"
  430. sleep 1
  431. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  432. if [ ${tmuxwc} -eq 1 ]; then
  433. fn_printok "Monitoring ${servicename}: Checking session: OK"
  434. fn_scriptlog "Checking session: OK"
  435. sleep 1
  436. echo -en "\n"
  437. fn_serverquery
  438. exit
  439. else
  440. fn_printfail "Monitoring ${servicename}: Checking session: FAIL"
  441. fn_scriptlog "Checking session: FAIL"
  442. sleep 1
  443. echo -en "\n"
  444. if [ "${emailnotification}" = "on" ]; then
  445. subject="${servicename} Monitor - Starting ${servername}"
  446. failurereason="${servicename} process not running"
  447. actiontaken="${servicename} has been restarted"
  448. fn_emailnotification
  449. fi
  450. fn_scriptlog "Monitor is starting ${servername}"
  451. fn_startserver
  452. fi
  453. else
  454. fn_printinfonl "Monitoring ${servicename}: Detected SteamCMD is checking for updates"
  455. fn_scriptlog "Detected SteamCMD is checking for updates"
  456. sleep 1
  457. fn_printinfonl "Monitoring ${servicename}: When updates complete ${servicename} will start"
  458. fn_scriptlog "When updates complete ${servicename} will start"
  459. sleep 1
  460. fi
  461. }
  462. fn_updateserver(){
  463. fn_rootcheck
  464. fn_syscheck
  465. fn_printdots "Updating ${servicename}: ${servername}"
  466. sleep 1
  467. fn_printok "Updating ${servicename}: ${servername}"
  468. sleep 1
  469. fn_scriptlog "Updating ${servername}"
  470. cd "${rootdir}"
  471. cd "steamcmd"
  472. ./steamcmd.sh +login ${steamuser} "${steampass}" +force_install_dir "${filesdir}" +app_update ${appid} +quit|tee -a "${scriptlog}"
  473. }
  474. fn_validateserver(){
  475. fn_rootcheck
  476. fn_syscheck
  477. fn_printwarn "Validating may overwrite some customised files."
  478. sleep 1
  479. echo -en "\n"
  480. echo -en "https://developer.valvesoftware.com/wiki/SteamCMD#Validate"
  481. sleep 5
  482. echo -en "\n"
  483. fn_printdots "Validating ${servicename}: ${servername}"
  484. sleep 1
  485. fn_printok "Validating ${servicename}: ${servername}"
  486. sleep 1
  487. fn_scriptlog "Validating ${servername}"
  488. cd "${rootdir}"
  489. cd "steamcmd"
  490. ./steamcmd.sh +login ${steamuser} "${steampass}" +force_install_dir "${filesdir}" +app_update ${appid} validate +quit|tee -a "${scriptlog}"
  491. }
  492. fn_restartserver(){
  493. fn_scriptlog "Restarting ${servername}"
  494. fn_stopserver
  495. fn_startserver
  496. }
  497. fn_stopserver(){
  498. fn_rootcheck
  499. fn_syscheck
  500. pid=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  501. fn_printdots "Stopping ${servicename}: ${servername}"
  502. fn_scriptlog "Stopping ${servername}"
  503. sleep 1
  504. if [ "${pid}" == "0" ]; then
  505. fn_printfail "Stopping ${servicename}: ${servername} is already stopped"
  506. fn_scriptlog "${servername} is already stopped"
  507. else
  508. tmux kill-session -t ${servicename}
  509. fn_printok "Stopping ${servicename}: ${servername}"
  510. fn_scriptlog "Stopped ${servername}"
  511. fi
  512. # Remove lock file
  513. rm -f ${lockselfname}
  514. sleep 1
  515. echo -en "\n"
  516. }
  517. fn_startserver(){
  518. fn_rootcheck
  519. fn_syscheck
  520. fn_autoip
  521. fn_parms
  522. fn_logmanager
  523. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -v failed|grep -E "^${servicename}:"|wc -l)
  524. if [ ${tmuxwc} -eq 0 ]; then
  525. fn_scriptlog "Rotating log files"
  526. if [ "${engine}" == "unreal2" ]; then
  527. mv "${gamelog}" "${gamelogdate}"
  528. fi
  529. mv "${scriptlog}" "${scriptlogdate}"
  530. mv "${consolelog}" "${consolelogdate}"
  531. fi
  532. fn_printdots "Starting ${servicename}: ${servername}"
  533. fn_scriptlog "Starting ${servername}"
  534. sleep 1
  535. if [ ${tmuxwc} -eq 1 ]; then
  536. fn_printinfo "Starting ${servicename}: ${servername} is already running"
  537. fn_scriptlog "${servername} is already running"
  538. sleep 1
  539. echo -en "\n"
  540. exit
  541. fi
  542. # Create lock file
  543. date > "${rootdir}/${lockselfname}"
  544. cd "${executabledir}"
  545. tmux new-session -d -s ${servicename} "${executable} ${parms}" 2> ${scriptlogdir}/.${servicename}-tmux-error.tmp
  546. tmux pipe-pane -o -t ${servicename} "exec cat >> '${consolelog}'"
  547. sleep 1
  548. tmuxwc=$(tmux list-sessions 2>&1|awk '{print $1}'|grep -E "^${servicename}:"|wc -l)
  549. if [ ${tmuxwc} -eq 0 ]; then
  550. fn_printfailnl "Starting ${servicename}: Failed to start ${servername}"
  551. echo -en " Check log files: ${rootdir}/log"
  552. fn_scriptlog "failed to start ${servername}"
  553. if [ -a ${scriptlogdir}/.${servicename}-tmux-error.tmp ]; then
  554. fn_scriptlog "tmux returned the following error"
  555. cat ${scriptlogdir}/.${servicename}-tmux-error.tmp >> ${scriptlog}
  556. fi
  557. else
  558. fn_printok "Starting ${servicename}: ${servername}"
  559. fn_scriptlog "Started ${servername}"
  560. fi
  561. rm ${scriptlogdir}/.${servicename}-tmux-error.tmp
  562. sleep 1
  563. echo -en "\n"
  564. }
  565. fn_details(){
  566. if [ ! -f ${rootdir}/functions/fn_details ]; then
  567. functionfile="fn_details"
  568. fn_fninstall
  569. fi
  570. source ${rootdir}/functions/fn_details
  571. }
  572. fn_fninstall(){
  573. cd ${rootdir}
  574. if [ ! -d "functions" ]; then
  575. mkdir functions
  576. fi
  577. echo ""
  578. echo "loading ${functionfile}..."
  579. cd functions
  580. wget --no-check-certificate -nv -N https://raw.githubusercontent.com/dgibbs64/linuxgameservers/master/functions/${functionfile}
  581. chmod +x ${functionfile}
  582. cd ${rootdir}
  583. sleep 1
  584. echo ""
  585. }
  586. #
  587. ## Installer
  588. #
  589. fn_header(){
  590. clear
  591. echo "================================="
  592. echo "${gamename}"
  593. echo "Linux Game Server Manager"
  594. echo "by Daniel Gibbs"
  595. echo "http://danielgibbs.co.uk"
  596. echo "================================="
  597. echo ""
  598. }
  599. fn_steamdl(){
  600. echo "Installing SteamCMD"
  601. echo "================================="
  602. cd "${rootdir}"
  603. mkdir -pv "steamcmd"
  604. sleep 1
  605. cd "steamcmd"
  606. if [ ! -f steamcmd.sh ]; then
  607. wget -nv -N http://media.steampowered.com/client/steamcmd_linux.tar.gz
  608. tar --verbose -zxf steamcmd_linux.tar.gz
  609. rm -v steamcmd_linux.tar.gz
  610. chmod +x steamcmd.sh
  611. sleep 1
  612. else
  613. echo ""
  614. echo "Steam already installed!"
  615. fi
  616. sleep 1
  617. echo ""
  618. }
  619. fn_steaminstall(){
  620. echo "Installing ${gamename} Server"
  621. echo "================================="
  622. sleep 1
  623. mkdir -pv "${filesdir}"
  624. cd "${rootdir}/steamcmd"
  625. STEAMEXE=steamcmd ./steamcmd.sh +login ${steamuser} "${steampass}" +force_install_dir "${filesdir}" +app_update ${appid} validate +quit
  626. echo ""
  627. echo "================================="
  628. while true; do
  629. read -p "Was the install successful? [y/N]" yn
  630. case $yn in
  631. [Yy]* ) break;;
  632. [Nn]* ) fn_retryinstall;;
  633. * ) echo "Please answer yes or no.";;
  634. esac
  635. done
  636. echo ""
  637. }
  638. fn_steamfix(){
  639. if [ ! -f ${rootdir}/functions/fn_steamfix ]; then
  640. functionfile="fn_steamfix"
  641. fn_fninstall
  642. fi
  643. source ${rootdir}/functions/fn_steamfix
  644. }
  645. fn_loginstall(){
  646. if [ ! -f ${rootdir}/functions/fn_loginstall ]; then
  647. functionfile="fn_loginstall"
  648. fn_fninstall
  649. fi
  650. source ${rootdir}/functions/fn_loginstall
  651. }
  652. fn_getquery(){
  653. echo "GameServerQuery"
  654. echo "============================"
  655. while true; do
  656. read -p "Do you want to install GameServerQuery? [y/N]" yn
  657. case $yn in
  658. [Yy]* ) cd "${rootdir}"; wget -nv -N "http://danielgibbs.co.uk/dl/gsquery.py"; chmod +x gsquery.py; break;;
  659. [Nn]* ) echo -e "Not installing GameServerQuery.";break;;
  660. * ) echo "Please answer yes or no.";;
  661. esac
  662. done
  663. echo ""
  664. }
  665. fn_retryinstall(){
  666. while true; do
  667. read -p "Retry install? [y/N]" yn
  668. case $yn in
  669. [Yy]* ) fn_install; exit;;
  670. [Nn]* ) echo Exiting; exit;;
  671. * ) echo "Please answer yes or no.";;
  672. esac
  673. done
  674. }
  675. fn_install(){
  676. fn_rootcheck
  677. fn_header
  678. if [ -d "${filesdir}" ]; then
  679. echo "${gamename} Server is already installed here:"
  680. pwd
  681. echo ""
  682. while true; do
  683. read -p "Continue [y/N]" yn
  684. case $yn in
  685. [Yy]* ) fn_header; break;;
  686. [Nn]* ) echo Exiting; return 1;;
  687. * ) echo "Please answer yes or no.";;
  688. esac
  689. done
  690. fi
  691. echo "Install Directory:"
  692. pwd
  693. echo ""
  694. while true; do
  695. read -p "Continue [y/N]" yn
  696. case $yn in
  697. [Yy]* ) break;;
  698. [Nn]* ) echo Exiting; return 1;;
  699. * ) echo "Please answer yes or no.";;
  700. esac
  701. done
  702. fn_header
  703. fn_steamdl
  704. fn_steaminstall
  705. fn_steaminstall
  706. fn_steaminstall
  707. fn_steaminstall
  708. fn_steamfix
  709. fn_loginstall
  710. fn_getquery
  711. echo "Configuring ${gamename} Server"
  712. echo "================================="
  713. sleep 1
  714. read -p "Enter server name: " servername
  715. read -p "Enter rcon password: " rconpass
  716. sleep 1
  717. echo "Creating server.cfg."
  718. touch "${defaultcfg}"
  719. echo "exec ${servicename}.cfg" > "${defaultcfg}"
  720. sleep 1
  721. echo "Creating ${servicename}.cfg config file."
  722. touch "${servercfgfullpath}"
  723. touch "${systemdir}/listip.cfg"
  724. touch "${systemdir}/banned.cfg"
  725. {
  726. echo -e "// Use this file to configure your DEDICATED server."
  727. echo -e "// This config file is executed on server start."
  728. echo -e ""
  729. echo -e "// disable autoaim"
  730. echo -e "sv_aim 0"
  731. echo -e ""
  732. echo -e "// disable clients' ability to pause the server"
  733. echo -e "pausable 0"
  734. echo -e ""
  735. echo -e "// default server name. Change to \"Bob's Server\", etc."
  736. echo -e "hostname \"${servername}\""
  737. echo -e ""
  738. echo -e "// maximum client movement speed "
  739. echo -e "sv_maxspeed 320"
  740. echo -e ""
  741. echo -e "// 20 minute timelimit"
  742. echo -e "mp_timelimit 20"
  743. echo -e ""
  744. echo -e "// cheats off"
  745. echo -e "sv_cheats 0"
  746. echo -e ""
  747. echo -e "// load ban files"
  748. echo -e "exec listip.cfg"
  749. echo -e "exec banned.cfg"
  750. echo -e ""
  751. echo -e "// rcon passsword"
  752. echo -e "rcon_password \"${rconpass}\""
  753. echo -e ""
  754. echo -e "// Server Password"
  755. echo -e "sv_password \"\""
  756. echo -e ""
  757. echo -e "// Server Logging"
  758. echo -e "log on"
  759. echo -e "sv_logbans 1"
  760. echo -e "sv_logecho 1"
  761. echo -e "sv_logfile 1"
  762. echo -e "sv_log_onefile 0"
  763. }|tee "${servercfgfullpath}" > /dev/null 2>&1
  764. sleep 1
  765. echo ""
  766. fn_header
  767. sleep 1
  768. fn_details
  769. sleep 1
  770. echo "================================="
  771. echo "Install Complete!"
  772. echo ""
  773. echo "To start server type:"
  774. echo "${selfname} start"
  775. echo ""
  776. }
  777. case "$1" in
  778. start)
  779. fn_startserver;;
  780. stop)
  781. fn_stopserver;;
  782. restart)
  783. fn_restartserver;;
  784. update)
  785. fn_updateserver;;
  786. update-restart)
  787. fn_stopserver
  788. fn_updateserver
  789. fn_startserver;;
  790. validate)
  791. fn_validateserver;;
  792. validate-restart)
  793. fn_stopserver
  794. fn_validateserver
  795. fn_startserver;;
  796. monitor)
  797. fn_monitorserver;;
  798. email-test)
  799. fn_emailtest;;
  800. details)
  801. fn_details;;
  802. backup)
  803. fn_backupserver;;
  804. console)
  805. fn_console;;
  806. debug)
  807. fn_debugserver;;
  808. install)
  809. fn_install;;
  810. *)
  811. echo "Usage: $0 {start|stop|restart|update|update-restart|validate|validate-restart|monitor|email-test|details|backup|console|debug|install}"
  812. exit 1;;
  813. esac
  814. exit