4
0

gmodserver 25 KB

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