简体   繁体   English

如何根据日志消息重新启动脚本

[英]How to restart script based on the log message

I have a java application and want to restart it when certain message appears in log the file.我有一个 java 应用程序,当某些消息出现在日志文件中时,我想重新启动它。 What I have tried so far is the following script:到目前为止,我尝试过的是以下脚本:

#!/bin/bash

java -jar app.jar > app.log 2>&1
tail -f app.log | while read LOGLINE
do
   [[ "${LOGLINE}" == *"channel incative"* ]] && pkill -P $$ java &&  java -jar app.jar > app.log 2>&1
done

I run the scritp using the following command我使用以下命令运行脚本

nohup sh app.sh &

But it doesnt work as expected.但它没有按预期工作。 Do you have any suggestion?你有什么建议吗?

ps the java application is a legacy one coming from 3rd party, so I cannot do the logic in the application code itself. ps java 应用程序是来自第三方的遗留应用程序,因此我无法在应用程序代码本身中执行逻辑。

When the "channel incative" string is found in the log, kill java and restart the script.当在日志中找到“channel incative”字符串时,杀死 java 并重新启动脚本。 The log file is cleared so that the "channel" string can be searched for again.日志文件被清除,以便可以再次搜索“通道”字符串。 Use sleep to give java process a little time to die.使用 sleep 给 java 进程一点时间死掉。

#!/bin/bash
sleep 1
java -jar app.jar > app.log 2>&1
rm app.log 2> /dev/null
touch app.log
tail -f app.log | while read LOGLINE
do
   [[ "${LOGLINE}" == *"channel incative"* ]] && pkill -P $$ java && exec app.sh
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM