当前位置:网站首页>Shell script implements application service log warehousing MySQL
Shell script implements application service log warehousing MySQL
2022-07-04 22:22:00 【Wu_ Candy】
Today I'd like to share a shell Scripting tools , adopt shell Script vs mysql The combination of , Store the error output log of a specific service into the specified mysql In the table , So as to facilitate the positioning and analysis of error problems .
On a daily basis , Often need and linux The system deals with , for example : Service deployment 、 Log and service status viewing , and shell Scripts are and linux A common method of interaction . The following code example is in shell Embedded in the script mysql The configuration information drops the error log information into the database .
shell The script is as follows :
#!/usr/bin/env bash
hostName="l-beta.io"
dbPort=3306
dbUser="beta"
dbPassword="123456"
dbName="test_log"
tableName="beta_error_log_record"
env="test1"
base="/home/q/www"
# Into the dead cycle
while [[ 1 ]]; do
# Loop de ergodic base Folder under directory ,-vE The last two are the items to be excluded
for name in `ls $base | grep -vE "default|genesis"`;
do
#echo $base/$name
# Determine if it's a directory
if [[ -d $base/$name ]]; then
# Define the previous minute of the current time , It is used to filter and match the log content
before=`date -d "-1 minute" "+%Y-%m-%d %H:%M:"`
# echo "Watching log $name in $before"
# Filter and obtain catalina.out Last minute in the file Exception and Error journal
result=`grep -C5 "$before" $base/$name/logs/catalina.out | grep -vE "DEBUG|skywalking|10.8.0.37:8800" | grep -C5 -E "Exception|ERROR" | sed $'s/\'/\"/g' `
# If result If it is zero, skip this cycle
if [[ -z "$result" ]]; then
continue;
fi
echo "-------------------------->"
# Splicing insert sql sentence
insert_sql="insert into $tableName (environment,project_name,content) values('$env','$name','\n\nException:\n\n$result')"
echo $insert_sql > tmp.sql
echo "***********start*************"
echo $insert_sql
# Start execution sql sentence
mysql -h$hostName -P$dbPort -u$dbUser -p$dbPassword $dbName -e "source tmp.sql"
echo "************end************"
fi
done
# Input sleep 60s
echo -e "sleep 60 seconds\n"
sleep 60s
done
above shell The key information in the script is marked with corresponding comments , If you need to practice , Can be mysql The corresponding configuration information and the monitored log service path can be changed and replaced with the target information .
end
边栏推荐
- Tiktok actual combat ~ the number of comments is updated synchronously
- Common shortcut keys for hbuilder x
- 面试题 01.01. 判定字符是否唯一
- 30余家机构联合发起数字藏品行业倡议,未来会如何前进?
- 如何实现轻松管理1500万员工?
- More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
- PostgreSQL服务端编程聚合和分组
- VS2019 C# release下断点调试
- i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
- Redis sentinel simply looks at the trade-offs between distributed high availability and consistency
猜你喜欢
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)
Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
TLA+ 入门教程(1):形式化方法简介
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
Tiktok actual combat ~ the number of comments is updated synchronously
Use blocconsumer to build responsive components and monitor status at the same time
什么是商业智能(BI),就看这篇文章足够了
随机推荐
Flask 上下文详解
不同环境相同配置项的内容如何diff差异?
What is business intelligence (BI), just look at this article is enough
i. Mx6ull driver development | 24 - platform based driver model lights LED
Bizchart+slider to realize grouping histogram
凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer
Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
HBuilder X 常用的快捷键
Locust性能测试 —— 环境搭建及使用
传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
Relational database
Scala download and configuration
阿里推出新品牌“瓴羊”,致力成为“数字化领头羊”
Convolutional neural network model -- lenet network structure and code implementation
PostgreSQL JOIN实践及原理
微服务--开篇
Interview question 01.01 Determine whether the character is unique
Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
283. Moving zero-c and language assisted array method
vim 从嫌弃到依赖(23)——最后的闲扯