shell脚本自动监控jenkins,进程死掉自动重启
Shell脚本如下:vim monitor_jenkins.sh#!/bin/bashwhile true # 无限循环flag=`ps -aux |grep "jenkins" |grep -v "grep" |wc -l`doif [[ $flag -eq 0 ]] # 判断进程数如果等于0,则启动jenkinsthen`nohup java -jar /root/Desktop/packag
Shell脚本如下:
vim monitor_jenkins.sh
#!/bin/bash
while true # 无限循环
flag=`ps -aux |grep "jenkins" |grep -v "grep" |wc -l`
do
if [[ $flag -eq 0 ]] # 判断进程数如果等于0,则启动jenkins
then
`nohup java -jar /root/Desktop/package/jenkins/jenkins.war --httpPort=18080 >output 2>&1 &` # 启动jenkins
echo `date` - "Jenkins restart" >> running.log # 将重启时间写入自定义的日志文件
else
echo "Jenkins is running..." >> /dev/null
fi
sleep 60s # 延迟60秒后进入下次循环
done
运行脚本:bash monitor_jenkins.sh &
命令末尾的 & 号,意思是将这个任务放到后台去执行。
那么如何停止脚本运行呢?
(1)首先查找运行脚本的进程PID号:
ps -aux |grep "bash monitor_jenkins.sh"
(2)终止脚本进程:
kill -9 进程PID号
对脚本做一些说明:
在windows下编写上传到linux报错
line 2: $'\r': command not found
执行:
sed -i 's/\r\n/\n/g' monitor_jenkins.sh
ps -aux | grep # 查找进程
参数:-aux 意思是显示所有包含其他使用者的进程。
ps -aux | grep "process_name"
若只执行这条命令,会导致出现一个 grep 进程,也就是说若只用上面的命令,会永远得到至少一个进程(grep进程),所以还需要用下面的命令,排除 grep 本身这个进程:
grep -v "grep"
最后再用 wc -l 命令统计进程数。
if 判断如果获得的进程数等于0,则证明 jenkins 服务没有运行,执行启动命令。
sleep命令可以用来将目前动作延迟一段时间
sleep 1s 延迟1秒
sleep 1m 延迟1分钟
sleep 1h 延迟1小时
sleep 1d 延迟1天
#!/bin/bash
while true # 无限循环
flag=`ps -aux |grep "jenkins" |grep -v "grep" |wc -l`
flag_webhook=`ps -aux |grep "webhook" |grep -v "grep" |wc -l`
flag_test=`ps -aux |grep "doctor-test-platform-0.0.1-SNAPSHOT.jar" |grep -v "grep" |wc -l`
do
if [[ $flag -eq 0 ]] # 判断进程数如果等于0,则启动jenkins
then
`nohup java -jar /root/Desktop/package/jenkins/jenkins.war --httpPort=18080 >output 2>&1 &` # 启动jenkins
echo `date` - "Jenkins restart" >> running.log # 将重启时间写入自定义的日志文件
elif [[ $flag_webhook -eq 0 ]] # 判断进程数如果等于0,则启动jenkins
then
`nohup python3 /root/Desktop/package/jenkins/tapd/live_bug_inform/webhook.py > /dev/null 2>&1 &` # 启动webhook
echo `date` - "webhook restart" >> running.log # 将重启时间写入自定义的日志文件
elif [[ $flag_test -eq 0 ]] # 判断进程数如果等于0,则启动jenkins
then
`nohup java -jar doctor-test-platform-0.0.1-SNAPSHOT.jar > robot.log &` # 启动doctor-test
echo `date` - "doctor-test-platform-0.0.1-SNAPSHOT.jar restart" >> running.log # 将重启时间写入自定义的日志文件
else
echo "doctor-test-platform-0.0.1-SNAPSHOT.jar, jenkins, webhook.py running..." >> /dev/null
fi
sleep 60s # 延迟60秒后进入下次循环
done
更多推荐
所有评论(0)