l4d2server 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #!/bin/bash
  2. # Left 4 Dead 2
  3. # Server Management Script
  4. # Author: Daniel Gibbs
  5. # Website: http://danielgibbs.co.uk
  6. # Version: 011214
  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="c5m1_waterfront"
  17. maxplayers="8"
  18. port="27015"
  19. clientport="27005"
  20. ip="0.0.0.0"
  21. # https://developer.valvesoftware.com/wiki/Command_Line_Options#Source_Dedicated_Server
  22. fn_parms(){
  23. parms="-game left4dead2 -strictportbind -ip ${ip} -port ${port} +clientport ${clientport} +map ${defaultmap} +servercfgfile ${servercfg} -maxplayers ${maxplayers}"
  24. }
  25. #### Advanced Variables ####
  26. # Steam
  27. appid="222860"
  28. # Server Details
  29. servicename="l4d2-server"
  30. gamename="Left 4 Dead 2"
  31. engine="source"
  32. # Directories
  33. rootdir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd )"
  34. selfname="$0"
  35. lockselfname=$(echo ".${servicename}.lock")
  36. filesdir="${rootdir}/serverfiles"
  37. systemdir="${filesdir}/left4dead2"
  38. executabledir="${filesdir}"
  39. executable="./srcds_run"
  40. servercfgdir="${systemdir}/cfg"
  41. servercfg="${servicename}.cfg"
  42. servercfgfullpath="${servercfgdir}/${servercfg}"
  43. defaultcfg="${servercfgdir}/server.cfg"
  44. backupdir="backups"
  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. # [ OK ]
  73. fn_printok(){
  74. echo -en "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  75. }
  76. fn_printoknl(){
  77. echo -e "\r\033[K[\e[0;32m OK \e[0;39m] $@"
  78. }
  79. # [ INFO ]
  80. fn_printinfo(){
  81. echo -en "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  82. }
  83. fn_printinfonl(){
  84. echo -e "\r\033[K[\e[0;36m INFO \e[0;39m] $@"
  85. }
  86. # [ WARN ]
  87. fn_printwarn(){
  88. echo -en "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  89. }
  90. fn_printwarnnl(){
  91. echo -e "\r\033[K[\e[1;33m WARN \e[0;39m] $@"
  92. }
  93. # [ .... ]
  94. fn_printdots(){
  95. echo -en "\r\033[K[ .... ] $@"
  96. }
  97. fn_rootcheck(){
  98. functionfile="${FUNCNAME}"
  99. fn_runfunction
  100. }
  101. fn_syscheck(){
  102. if [ ! -e "${systemdir}" ]; then
  103. fn_printfailnl "Cannot access ${systemdir}: No such directory"
  104. exit
  105. fi
  106. }
  107. fn_autoip(){
  108. functionfile="${FUNCNAME}"
  109. fn_runfunction
  110. }
  111. fn_logmanager(){
  112. functionfile="${FUNCNAME}"
  113. fn_runfunction
  114. }
  115. fn_debugserver(){
  116. functionfile="${FUNCNAME}"
  117. fn_runfunction
  118. }
  119. fn_console(){
  120. functionfile="${FUNCNAME}"
  121. fn_runfunction
  122. }
  123. fn_backupserver(){
  124. functionfile="${FUNCNAME}"
  125. fn_runfunction
  126. }
  127. fn_distro(){
  128. functionfile="${FUNCNAME}"
  129. fn_runfunction
  130. }
  131. fn_uptime(){
  132. functionfile="${FUNCNAME}"
  133. fn_runfunction
  134. }
  135. fn_load(){
  136. functionfile="${FUNCNAME}"
  137. fn_runfunction
  138. }
  139. fn_emailnotification(){
  140. functionfile="${FUNCNAME}"
  141. fn_runfunction
  142. }
  143. fn_emailtest(){
  144. functionfile="${FUNCNAME}"
  145. fn_runfunction
  146. }
  147. fn_serverquery(){
  148. functionfile="${FUNCNAME}"
  149. fn_runfunction
  150. }
  151. fn_monitorserver(){
  152. functionfile="${FUNCNAME}"
  153. fn_runfunction
  154. }
  155. fn_updateserver(){
  156. functionfile="${FUNCNAME}"
  157. fn_runfunction
  158. }
  159. fn_validateserver(){
  160. functionfile="${FUNCNAME}"
  161. fn_runfunction
  162. }
  163. fn_restartserver(){
  164. fn_scriptlog "Restarting ${servername}"
  165. fn_stopserver
  166. fn_startserver
  167. }
  168. fn_stopserver(){
  169. functionfile="${FUNCNAME}"
  170. fn_runfunction
  171. }
  172. fn_startserver(){
  173. functionfile="${FUNCNAME}"
  174. fn_runfunction
  175. }
  176. fn_details(){
  177. functionfile="${FUNCNAME}"
  178. fn_runfunction
  179. }
  180. fn_runfunction(){
  181. # Download function if missing
  182. if [ ! -f "${rootdir}/functions/${functionfile}" ]; then
  183. cd "${rootdir}"
  184. if [ ! -d "functions" ]; then
  185. mkdir functions
  186. fi
  187. echo "loading ${functionfile}..."
  188. cd functions
  189. wget --no-check-certificate -nv -N https://raw.githubusercontent.com/dgibbs64/linuxgameservers/master/functions/${functionfile}
  190. chmod +x "${functionfile}"
  191. cd "${rootdir}"
  192. sleep 1
  193. echo ""
  194. fi
  195. # Run function
  196. source "${rootdir}/functions/${functionfile}"
  197. }
  198. #
  199. ## Installer
  200. #
  201. fn_header(){
  202. functionfile="${FUNCNAME}"
  203. fn_runfunction
  204. }
  205. fn_steamdl(){
  206. functionfile="${FUNCNAME}"
  207. fn_runfunction
  208. }
  209. fn_steaminstall(){
  210. functionfile="${FUNCNAME}"
  211. fn_runfunction
  212. }
  213. fn_steamfix(){
  214. functionfile="${FUNCNAME}"
  215. fn_runfunction
  216. }
  217. fn_loginstall(){
  218. functionfile="${FUNCNAME}"
  219. fn_runfunction
  220. }
  221. fn_getquery(){
  222. functionfile="${FUNCNAME}"
  223. fn_runfunction
  224. }
  225. fn_retryinstall(){
  226. functionfile="${FUNCNAME}"
  227. fn_runfunction
  228. }
  229. fn_serverdirectory(){
  230. functionfile="${FUNCNAME}"
  231. fn_runfunction
  232. }
  233. fn_serverconfig(){
  234. functionfile="${FUNCNAME}"
  235. fn_runfunction
  236. }
  237. fn_install(){
  238. fn_rootcheck
  239. fn_header
  240. if [ -z "${autoinstall}" ]; then
  241. fn_serverdirectory
  242. fn_header
  243. fi
  244. fn_steamdl
  245. fn_steaminstall
  246. fn_steamfix
  247. fn_loginstall
  248. fn_getquery
  249. fn_serverconfig
  250. fn_header
  251. fn_details
  252. echo "================================="
  253. echo "Install Complete!"
  254. echo ""
  255. echo "To start server type:"
  256. echo "${selfname} start"
  257. echo ""
  258. }
  259. fn_autoinstall(){
  260. autoinstall=1
  261. fn_install
  262. }
  263. case "$1" in
  264. start)
  265. fn_startserver;;
  266. stop)
  267. fn_stopserver;;
  268. restart)
  269. fn_restartserver;;
  270. update)
  271. fn_updateserver;;
  272. update-restart)
  273. fn_stopserver
  274. fn_updateserver
  275. fn_startserver;;
  276. validate)
  277. fn_validateserver;;
  278. validate-restart)
  279. fn_stopserver
  280. fn_validateserver
  281. fn_startserver;;
  282. monitor)
  283. fn_monitorserver;;
  284. email-test)
  285. fn_emailtest;;
  286. details)
  287. fn_details;;
  288. backup)
  289. fn_backupserver;;
  290. console)
  291. fn_console;;
  292. debug)
  293. fn_debugserver;;
  294. install)
  295. fn_install;;
  296. auto-install)
  297. fn_autoinstall;;
  298. *)
  299. echo "Usage: $0 {start|stop|restart|update|update-restart|validate|validate-restart|monitor|email-test|details|backup|console|debug|install|auto-install}"
  300. exit 1;;
  301. esac
  302. exit