Linux Shell 脚本监控进程状态
Linux Shell cron 大约 1411 字背景
此处以监测Elasticsearch
进程是否存活为例。
cron 定时任务
- 优点:系统进程,不易被回收
- 缺点:频率最低为一分钟执行一次
脚本
注意:监控其他服务只需要替换
java
为对应的进程即可,如替换为进程中包含mongod-shardsvr1.conf
字符的进程。
#!/bin/bash
count=`ps aux | grep 'java' | grep -v 'grep' -c`
if [ $count -eq 0 ]; then
now=`date +%F\ %T`
echo "[$now] Elasticseach is offline, try to restart..." >> /elasticsearch/check_es.log
/elasticsearch/elasticsearch-7.0.0/bin/elasticsearch -d
else
now=`date +%F\ %T`
echo "[$now] Elasticsearch is online, everything seems to be OK..." >> /elasticsearch/check_es.log
fi
cron 配置
注意:一些进程可能需要
root
用户才能启动,所以可能cron
也需切换到root
用户才能生效。
查看现有定时任务
crontab -l
编辑定时任务
crontab -e
设置每分钟执行一次脚本
* * * * * /bin/bash /elasticsearch/check_es.sh
查看定时任务执行日志
tail -f /var/log/cron
查看脚本执行日志(自定义输出的日志)
tail -f /elasticsearch/check_es.log
后台 shell 进程
- 优点:频率可最低设置为一秒一次
- 缺点:作为用户级别的后台进程,可能被回收
脚本
while true;
do
count=`ps aux | grep -v 'grep' | grep -c 'java'`
if [ $count -eq 0 ]; then
now=`date +%F\ %T`
echo "[$now] Elasticseach is offline, try to restart..." >> /elasticsearch/check_es.log
/elasticsearch/elasticsearch-7.0.0/bin/elasticsearch -d
else
now=`date +%F\ %T`
echo "[$now] Elasticsearch is online, everything seems to be OK..." >> /elasticsearch/check_es.log
fi
sleep 1
done
执行 shell
nohup ./monitor.sh >/dev/null 2>&1 &
阅读 4793 · 发布于 2019-08-10
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓
昵称:
随便看看
换一批
-
Linux中/bin、/sbin、/usr/bin、/usr/sbin、/usr/local/bin、/usr/local/sbin 目录的含义及区别阅读 1563
-
Spring 使用 filter 过滤器、ContentCachingWrapper 包装类获取请求参数和返回值阅读 1909
-
PostgreSQL 修改字段属性阅读 2886
-
Android 根据包名启动 Activity阅读 3327
-
Flowable 配置单独数据库阅读 62
-
Java 进程间通讯的几种方式阅读 2981
-
走进 Rust:引用的生命周期阅读 1806
-
Windows10 访问网页提示找不到服务器 IP 地址阅读 10924
-
IDEA TestMe 插件快速生成 Unit Test 单元测试阅读 6379
-
数据结构:满二叉树和完全二叉树阅读 2548