logs.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 modulename="LOGS"
  8. local commandaction="Log-Manager"
  9. # Check if logfile variable and file exist, create logfile if it doesn't exist.
  10. if [ "${consolelog}" ]; then
  11. if [ ! -f "${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 [ "$(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. fn_sleep_time
  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. fn_sleep_time
  40. fn_print_info "Removing logs older than ${logdays} days"
  41. fn_script_log_info "Removing logs older than ${logdays} days"
  42. # Logging logfiles to be removed according to "${logdays}", counting and removing them.
  43. # Script logfiles.
  44. find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  45. scriptcount=$(find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  46. find "${lgsmlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  47. # SRCDS and unreal logfiles.
  48. if [ "${engine}" == "unreal2" ]||[ "${engine}" == "source" ]; then
  49. find "${gamelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  50. gamecount=$(find "${gamelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  51. find "${gamelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  52. fi
  53. # Console logfiles.
  54. if [ "${consolelog}" ]; then
  55. find "${consolelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  56. consolecount=$(find "${consolelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  57. find "${consolelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  58. fi
  59. # Common logfiles.
  60. if [ -d "${commonlogs}" ]; then
  61. find "${commonlogs}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  62. smcount=$(find "${commonlogs}"/ -type f -mtime +"${logdays}" | wc -l)
  63. find "${commonlogs}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  64. fi
  65. if [ -d "${commonsourcelogs}" ]; then
  66. find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  67. smcount=$(find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | wc -l)
  68. find "${commonsourcelogs}"/* -mtime +"${logdays}" -type f -exec rm -f {} \;
  69. fi
  70. # Source addons logfiles.
  71. if [ "${engine}" == "source" ]; then
  72. # SourceMod logfiles.
  73. if [ -d "${sourcemodlogdir}" ]; then
  74. find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  75. smcount=$(find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  76. find "${sourcemodlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  77. fi
  78. # Garry's Mod logfiles.
  79. if [ "${shortname}" == "gmod" ]; then
  80. # ULX logfiles.
  81. if [ -d "${ulxlogdir}" ]; then
  82. find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  83. ulxcount=$(find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  84. find "${ulxlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  85. fi
  86. # DarkRP logfiles.
  87. if [ -d "${darkrplogdir}" ]; then
  88. find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  89. darkrpcount=$(find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  90. find "${darkrplogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  91. fi
  92. fi
  93. fi
  94. # Count total amount of files removed.
  95. countlogs=$((scriptcount + consolecount + gamecount + srcdscount + smcount + ulxcount + darkrpcount))
  96. # Job done.
  97. fn_print_ok "Removed ${countlogs} log files"
  98. fn_script_log "Removed ${countlogs} log files"
  99. fi