gmodserver 22 KB

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