csserver 23 KB

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