logs.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. # LinuxGSM logs.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://linuxgsm.com
  6. # Description: Acts as a log rotator, removing old logs.
  7. local commandname="LOGS"
  8. local commandaction="Log-Manager"
  9. # Check if logfile variable and file exist, create logfile if it doesn't exist
  10. if [ -n "${consolelog}" ]; then
  11. if [ ! -e "${consolelog}" ]; then
  12. touch "${consolelog}"
  13. fi
  14. fi
  15. # For games not displaying a console, and having logs into their game directory
  16. check_status.sh
  17. if [ "${status}" != "0" ] && [ "${function_selfname}" == "command_start.sh" ] && [ -n "${gamelogfile}" ]; then
  18. if [ -n "$(find "${systemdir}" -name "gamelog*.log")" ]; then
  19. fn_print_info "Moving game logs to ${gamelogdir}"
  20. fn_script_log_info "Moving game logs to ${gamelogdir}"
  21. echo -en "\n"
  22. sleep 0.5
  23. mv "${systemdir}"/gamelog*.log "${gamelogdir}"
  24. fi
  25. fi
  26. # Log manager will start the cleanup if it finds logs older than "${logdays}"
  27. if [ $(find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | wc -l) -ne "0" ]; then
  28. fn_print_dots "Starting"
  29. # Set common logs directories
  30. commonlogs="${systemdir}/logs"
  31. commonsourcelogs="${systemdir}/*/logs"
  32. # Set addon logs directories
  33. sourcemodlogdir="${systemdir}/addons/sourcemod/logs"
  34. ulxlogdir="${systemdir}/data/ulx_logs"
  35. darkrplogdir="${systemdir}/data/darkrp_logs"
  36. legacyserverlogdir="${logdir}/server"
  37. # Setting up counting variables
  38. scriptcount="0" ; consolecount="0" ; gamecount="0" ; srcdscount="0" ; smcount="0" ; ulxcount="0" ; darkrpcount="0" ; legacycount="0"
  39. sleep 0.5
  40. fn_print_ok_nl "Starting"
  41. fn_print_info_nl "Removing logs older than ${logdays} days"
  42. fn_script_log_info "Removing logs older than ${logdays} days"
  43. # Logging logfiles to be removed according to "${logdays}", counting and removing them
  44. # Script logfiles
  45. find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  46. scriptcount=$(find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  47. find "${lgsmlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  48. # SRCDS and unreal logfiles
  49. if [ "${engine}" == "unreal2" ]||[ "${engine}" == "source" ]; then
  50. find "${gamelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  51. gamecount=$(find "${gamelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  52. find "${gamelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  53. fi
  54. # Console logfiles
  55. if [ -n "${consolelog}" ]; then
  56. find "${consolelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  57. consolecount=$(find "${consolelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  58. find "${consolelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  59. fi
  60. # Common logfiles
  61. if [ -d "${commonlogs}" ]; then
  62. find "${commonlogs}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  63. smcount=$(find "${commonlogs}"/ -type f -mtime +"${logdays}" | wc -l)
  64. find "${commonlogs}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  65. fi
  66. if [ -d "${commonsourcelogs}" ]; then
  67. find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  68. smcount=$(find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | wc -l)
  69. find "${commonsourcelogs}"/* -mtime +"${logdays}" -type f -exec rm -f {} \;
  70. fi
  71. # Source addons logfiles
  72. if [ "${engine}" == "source" ]; then
  73. # SourceMod logfiles
  74. if [ -d "${sourcemodlogdir}" ]; then
  75. find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  76. smcount=$(find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  77. find "${sourcemodlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  78. fi
  79. # Garry's Mod logfiles
  80. if [ "${gamename}" == "Garry's Mod" ]; then
  81. # ULX logfiles
  82. if [ -d "${ulxlogdir}" ]; then
  83. find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  84. ulxcount=$(find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  85. find "${ulxlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  86. fi
  87. # DarkRP logfiles
  88. if [ -d "${darkrplogdir}" ]; then
  89. find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  90. darkrpcount=$(find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  91. find "${darkrplogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  92. fi
  93. fi
  94. fi
  95. # Count total amount of files removed
  96. countlogs=$((${scriptcount} + ${consolecount} + ${gamecount} + ${srcdscount} + ${smcount} + ${ulxcount} + ${darkrpcount}))
  97. # Job done
  98. fn_print_ok_nl "Removed ${countlogs} log files"
  99. fn_script_log "Removed ${countlogs} log files"
  100. fi