bsserver 22 KB

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