gmodserver 25 KB

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