fofserver 23 KB

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