gpt4 book ai didi

linux - 用于检查日志是否针对当前时间戳生成的 Shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:11 36 4
gpt4 key购买 nike

要求是检查日志文件是否针对当前时间戳生成。

如果是:不执行任何操作。如果否:则检查java进程(ABC.jar),杀死现有进程并启动新进程。

下面是我试图执行并得到以下错误的脚本。

请帮助我解决问题和更好的实现方式。

脚本。

#!/bin/sh
NOTIFYJARFILE='ABC.jar'
cd /test/script
echo "Inside the directory"
ls -ltrh | tail -1
echo "Files are listed"
PERVIOUS_TIME = cat 'test.log' | date "+%D %T"
echo "PERVIOUS_TIME"
CURRENT_TIME = date "+%D %T"
echo "CURRENT_TIME"
if [ $CURRENT_TIME -eq $PERVIOUS_TIME ]
then
echo "Logs are generating for the currentTimestamp | `date`"
else
echo "Logs are Not generating for the currentTimestamp | `date`"
#Checking the running ABC Jar process
PID=ps ax | grep -v grep | grep ABC.jar | awk '{ print $1 }'
kill -9 $PID
#starting the Notify Jar process
echo "Starting the ABC jar | `date`"
java -jar /test/script/$NOTIFYJARFILE > /dev/null 2>&1 &
fi

错误:

Inside the directory
test.log
Files are listed
Notify.sh: line 7: test.log: command not found
07/30/19 14:10:04
PERVIOUS_TIME
Notify.sh: line 9: +%D %T: command not found
CURRENT_TIME
Inside If loop
Logs are generating for the current timestamp | Tue Jul 30 14:10:04 EAT 2019

最佳答案

只要尝试这种方式分配变量,你的变量应该知道它是一个命令输出,所以:

PERVIOUS_TIME=$(cat 'test.log' | date "+%D %T")

CURRENT_TIME=$(date "+%D %T")

或者这样:

PERVIOUS_TIME=`cat 'test.log' | date "+%D %T"`

CURRENT_TIME=`date "+%D %T"`

关于linux - 用于检查日志是否针对当前时间戳生成的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57270818/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com