Index: common.scm ================================================================== --- common.scm +++ common.scm @@ -525,16 +525,20 @@ ;; (print-call-chain (current-error-port)) ;; ) (let* ((fullname (conc "logs/" file)) (mod-time (file-modification-time fullname)) (file-age (- (current-seconds) mod-time)) - (file-old (> file-age (* 48 60 60))) + (file-old (> file-age (* 48 60 60))) ;; over 48 hours (file-big (> (file-size fullname) 200000))) (hash-table-set! all-files file mod-time) - (if (or (and (string-match "^.*.log" file) + (if (or + ;; gzip: + ;; any old and big log files: (server logs, runlogs, update_ext_specs, etc. + (and (string-match "^.*.log" file) file-old file-big) + ;; old server log files: (and (string-match "^server-.*.log" file) file-old)) (let ((gzfile (conc fullname ".gz"))) (if (common:file-exists? gzfile) (begin @@ -546,10 +550,11 @@ (system (conc "gzip " fullname)) (inc-stat "gzipped") (hash-table-set! all-files (conc file ".gz") file-age) ;; add the .gz file and remove the base file (hash-table-delete! all-files file) ) + ;; delete other files over expiration date: (if (and (> file-age (* (string->number (or (configf:lookup *configdat* "setup" "log-expire-days") "30")) 24 3600)) (file-exists? fullname)) ;; just in case it was gzipped - will get it next time (handle-exceptions exn #f @@ -575,17 +580,35 @@ (lambda (a b) (< (hash-table-ref all-files a)(hash-table-ref all-files b)))) (- num-logs max-allowed)))) (for-each (lambda (file) - (let* ((fullname (conc "logs/" file))) + (let* ((fullname (conc "logs/" file)) + (is-alive 0)) + ;; Don't delete it if it's the log file of a running server. + (if (string-match "server-\\d+-[a-zA-Z0-9]+\\.log" file) + (let* ((parts (string-split file "-.")) + (pid (cadr parts)) ; Second part is the PID + (server-machine (caddr parts)) ; Third part is the server machine + (local (string=? (get-host-name) server-machine)) + (test-cmd (conc "test -d /proc/" pid))) + (if local + (set! is-alive (not (system test-cmd))) + (set! is-alive (not (system (conc "ssh " server-machine " test -d /proc/" pid)))) + ) + ) + ) (if (directory? fullname) (debug:print-info 0 *default-log-port* fullname " in logs directory is a directory! Cannot rotate it, it is best to not put subdirectories in the logs dir.") (handle-exceptions exn (debug:print-error 0 *default-log-port* "failed to remove " fullname ", exn=" exn) - (delete-file* fullname))))) + (if (not is-alive) + (delete-file* fullname) + (debug:print-info 0 *default-log-port* "Not deleting log file " file " since its server is still alive") + ) + )))) files) (debug:print-info 0 *default-log-port* "Deleted " (length files) " files from logs, keeping " max-allowed " files.")))))) ;;====================================================================== ;; (begin