gmodserver 6.4 KB

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