使用rotatelogs旋转catalina.out?

时间:2017-01-26 17:20:22

标签: linux apache tomcat tomcat7

我正在尝试旋转catalina.out,当它超过我帮助开发的专用安全设备的最大大小时。我们目前使用logrotate执行此任务,但是我试图使文件在达到大小限制时立即旋转,而不是依赖于每日cron作业。

根据the Tomcat FAQ,我应该尝试修补catalina.sh以将输出管道输出到命令like so。我们在这个设备上没有cronolog,但是我们确实有httpd所以我决定使用rotatelogs

CATALINA_OUT_CMD="/usr/sbin/rotatelogs -f $CATALINA_OUT 100M >/dev/null 2>&1"

问题是catalina.out甚至没有在那里创建。我该怎么办?

这是补丁文件的 my 版本的一部分,它支持管道命令:

@@ -33,6 +33,13 @@
#                   will be redirected.
#                   Default is $CATALINA_BASE/logs/catalina.out
#
+#   CATALINA_OUT_CMD (Optional) Command which will be executed and receive
+#                   as its stdin the stdout and stderr from the Tomcat java
+#                   process.  Only used if CATALINA_OUT is not defined.
+#                   No default.
+#                   Example (all one line)
+#                   CATALINA_OUT_CMD="cronolog $CATALINA_BASE/logs/catalina.%Y-%m-%d.out >/dev/null 2>&1"
+#
#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
#                   "run" or "debug" command is executed.
#                   Include here and not in JAVA_OPTS all options, that should
@@ -367,13 +374,22 @@ elif [ "$1" = "start" ] ; then
fi

shift
-  touch "$CATALINA_OUT"
+  if [ -z "$CATALINA_OUT_CMD" ] ; then
+    catalina_out_command=">> \"$CATALINA_OUT\" 2>&1"
+  else
+    catalina_out_command="| $CATALINA_OUT_CMD"
+  fi
+  if [ ! -z "$CATALINA_PID" ]; then
+    catalina_pid_file="$CATALINA_PID"
+  else
+    catalina_pid_file=/dev/null
+  fi
if [ "$1" = "-security" ] ; then
if [ $have_tty -eq 1 ]; then
echo "Using Security Manager"
fi
shift
-    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
+    eval \{ "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Djava.security.manager \
-Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \
@@ -381,21 +397,17 @@ elif [ "$1" = "start" ] ; then
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start \
-      >> "$CATALINA_OUT" 2>&1 "&"
+      2\>\&1 \& echo \$! \>\"$catalina_pid_file\" \; \} $catalina_out_command "&"

else
-    eval "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
+    eval \{ "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \
-Djava.endorsed.dirs="\"$JAVA_ENDORSED_DIRS\"" -classpath "\"$CLASSPATH\"" \
-Dcatalina.base="\"$CATALINA_BASE\"" \
-Dcatalina.home="\"$CATALINA_HOME\"" \
-Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \
org.apache.catalina.startup.Bootstrap "$@" start \
-      >> "$CATALINA_OUT" 2>&1 "&"
-
-  fi
+      2\>\&1 \& echo \$! \>\"$catalina_pid_file\" \; \} $catalina_out_command "&"

-  if [ ! -z "$CATALINA_PID" ]; then
-    echo $! > "$CATALINA_PID"
fi

echo "Tomcat started."

P.S。如果它很重要,我正在使用以下修改过的" out命令测试旋转"所以我不必等待日志填满:

CATALINA_OUT_CMD="/usr/sbin/rotatelogs -f $CATALINA_OUT 1M >/dev/null 2>&1"

P.P.S。如果你们问我的话,我会愿意再次尝试log4j,但是上次我尝试的时候,我没能完成这项工作。

1 个答案:

答案 0 :(得分:1)

好问题。我不知道在某个文件大小强制轮换的方法。这是有道理的,因为这意味着应用程序必须不断运行和检查......但是,您可以增加logrotate运行和设置大小限制的频率。如果它每15分钟运行一次,允许的最大大小为50MB,那么它最多只能在15分钟内超过50 MB。

您可以设置它在cron中运行的频率。