1 Star 4 Fork 1

无念 / auto-monitor-java-process

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

通过Shell脚本自动监控JAVA进程中线程cpu使用率

本文主要介绍在 show-busy-java-threads.sh 脚本的功能基础上,通过 process-cpu-monitor.sh 脚本实现Linux平台上Java进程或服务的线程繁忙情况监控。

show-busy-java-threads.sh是一个能够打印Java进程中线程cpu使用率的脚本,可以用来排查程序cpu使用率异常的情况。详细使用方法-->传送门

尽管show-busy-java-threads.sh功能很强大,但是它不支持cpu使用率监控功能。一般都是cpu开始报警之后才人为去触发,就显得为时已晚,往往就错过了最想要的数据。

process-cpu-monitor.sh脚本就实现了对进程cpu使用率的监控,当监控到进程cpu使用率超过设定的值时,就执行show-busy-java-threads.sh脚本来实时抓取进程中线程cpu的使用情况。

使用说明

脚本可接收两个参数,首个参数表示要监控的进程(必传),值可以为进程pid值pid文件或者服务名。第二个参数是监控的cpu使用率(可选),0~100数值,表示超过设定的值就执行show-busy-java-threads.sh脚本,默认为95。

脚本中的busyThreadLogbusyThreadPid定义了执行show-busy-java-threads.sh脚本进程的pid和线程监控情况的输出,使用前请自行修改。同时其执行参数默认如下:

nohup sh show-busy-java-threads.sh 5 60 -c 20 -p $pid

process-cpu-monitor.sh用法

 process-cpu-monitor.sh 32145
 # 监控进程PID为32145的java服务

 process-cpu-monitor.sh /temp/xxxpid
 # /temp/xxxpid 表示进程pid文件,文件内容进程pid值

 process-cpu-monitor.sh mall-service
 # mall-service Java服务名,可能是main类名的一部分或执行jar或war名包的一部分

 process-cpu-monitor.sh 32145 80
 # 监控进程PID为32145的java服务,当进程cpu使用超过80%时,开始执行监控线程的脚本

 nohup ./process-cpu-monitor.sh mall-service >/dev/null 2>&1 &
 # 以守护进程的方式执行脚本,/dev/null可以替换想要输出的文件
 

开机自启动

  1. 通过修改/etc/rc.d/rc.local文件来实现启动执行监控脚本
  2. 在使用systemd的系统上,创建一个systemd的启动服务来执行脚本
  3. 使用crontab来开机启动执行脚本
MIT License Copyright (c) 2021 立志尚早 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

自动监控java进程cpu使用率及进程中线程cpu占用情况 展开 收起
Shell
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Shell
1
https://gitee.com/jialy/auto-monitor-java-process.git
git@gitee.com:jialy/auto-monitor-java-process.git
jialy
auto-monitor-java-process
auto-monitor-java-process
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891