【问题标题】:Create daily log script in linux在linux中创建每日日志脚本
【发布时间】:2020-12-19 20:50:24
【问题描述】:

我正在尝试编写一个脚本来生成每日日志文件,但目前,日志文件将所有日志附加到一个文件中,即 test.log。我能够生成带有日期扩展名的 test.log,但它只是更改了日期,并且 test.log 中的日志具有所有天数我如何编写一个脚本来生成带有日期扩展名的每日日志。

NOW=$(date +"%F")
LOG_DIR=$OO_LOCAL{log-dir}
#CONSOLE_LOG="$OO_LOCAL{console-log}-$NOW.log"
CONSOLE_LOG="/log/springboot/test-$NOW.log"
TMP_DIR=$DIR/tmp
JAR_FILE="/app/$OO_LOCAL{artifact-id}/current/$OO_LOCAL{artifact-id}-$OO_LOCAL{artifact-version}.jar"

CMD="java $JAVA_OPTS $DISABLE_JIT -Djava.io.tmpdir=$TMP_DIR -jar $JAR_FILE 2>&1 >> $CONSOLE_LOG & echo \$! >$PIDFILE"

输出 -rw-r--r-- 1 root root 8453324 Jun 30 11:38 test-2020-06-29.log -rw-r--r-- 1 root root 11864615 Jul 2 15:36 test-2020-06-30.log -rw-r--r-- 1 root root 1179215 Jul 3 12:25 test-2020-07-02.log

【问题讨论】:

  • NOW=$(date +"%F") LOG_DIR=$OO_LOCAL{log-dir} #CONSOLE_LOG="$OO_LOCAL{console-log}-$NOW.log" CONSOLE_LOG="/log /springboot/intl-e2e-sc-visibility-kpi-$NOW.log" TMP_DIR=$DIR/tmp JAR_FILE="/app/$OO_LOCAL{artifact-id}/current/$OO_LOCAL{artifact-id}-$OO_LOCAL {artifact-version}.jar" PROG="$OO_LOCAL{name}" PIDFILE="$OO_LOCAL{pid-location}" 我是新手,不知道如何设置 cron 作业。

标签: linux logging scripting


【解决方案1】:

首先,如果您制作了一个完整的示例,这将有所帮助,因为有些变量的定义并不明显。在您的 CMD 行中,您使用的是附加运算符而不是覆盖运算符(请参阅this answer),但这只是问题的一部分。您应该首先研究为您希望重复发生的任何事件创建和测试 cron 作业。请参阅 cron jobsproperly testing cron 作业。

以下是我过去如何为文件添加日期。这些行属于一个文件,该文件是每晚由 cron 作业运行的脚本的一部分。

# get current directory name
ABSDIR=`pwd`
DIR=`basename $ABSDIR`
# form the name of the tarball from the directory name and the date
TBNAME="$DIR"-`date +"%F.%H.%M.%S"`.tgz

当我想创建带时间戳的 tarball 时,我经常使用脚本本身。

有些人可能不喜欢这种格式,在这种情况下,您可以将反引号转换为 $()​​ 例如:

# get current directory name
ABSDIR=$(pwd)
DIR=$(basename $ABSDIR)
# form the name of the tarball from the directory name and the date
TBNAME="$DIR"-$(date +"%F.%H.%M.%S").tgz

【讨论】:

  • 我有输出文件,但它每天都在附加,但我想每天创建一个带有日期的新文件。知道如何实现吗?这是现有的脚本
猜你喜欢
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-10
  • 2018-02-15
相关资源
最近更新 更多