一、背景:
公司的itsm服务台系统的后端应用经常半夜出现问题,java进程经常挂掉,并且该系统也是最近需要下线的,所以需要就配置一个自检和自启脚本,这样的话就提高了工作效率,同时也避免了半夜应用挂掉,需要联系运维人员起来启动应用服务。
二、脚本需求:
1、脚本需要判断java进程是否存在;
2、需要做循环判断处理;
3、执行的过程需要写入到指定的日志文件中;
4、需要判断脚本执行的用户;
三、脚本撰写:
vim start_itsm.sh
#!/bin/bash
#aouth:ex-xionghj001
#time:2023/3/9
log_output () {
if [ ! -f /root/bin/start-log.log ]
then
cd /root/bin/
touch /root/bin/start-log.log
fi
echo -e "\e[$1m[$(date +"%F %T")] $2 \e[0m"
}
start_itsm () {
user=`whoami`
if [ $user == root ]
then
log_output 31 "[ERROR] The is test" | tee -a /root/bin/start-log.log
docker-deploycompose -s balancer -a restartall $>> /root/bin/start-log.log
else
log_output 31 "[ERROR] The current user is not root,need user is root" | tee -a /root/bin/start-log.log
exit
fi
}
for ((i=10;i>0;i--))
do
itsm=`ps -ef |grep java | grep -v grep| wc -l`
if [ $itsm -gt 0 ]
then
log_output 32 "[INFO] itsm is running" | tee -a /root/bin/start-log.log
exit
else
log_output 31 "[ERROR] itsm is not running, need running" | tee -a /root/bin/start-log.log
start_itsm
fi
log_output 32 "For loop $i" | tee -a /root/bin/start-log.log
done
四、脚本授权并设置定时任务:
# chmod 755 start_itsm.sh
# crontab -e
*/2 * * * * /bin/bash /root/bin/start_itsm.sh