linux 自动重启服务 shell
linux下监视进程 崩溃挂掉后自动重启的shell脚本 - Python技术站 (pythonjishu.com)
新建 日志存放目录:
mkdir /mnt/data/logs/monitor -p
脚本路径:
/mnt/data/zzy/scripts
案例一: 监控nginx
1:脚本:cat monitor_nginx.sh
#!/bin/bash
PROCESS_NAME="nginx"
# 使用pgrep命令来查找进程id
PROCESS_ID=$(pgrep "${PROCESS_NAME}")
if [ "${PROCESS_ID}" = "" ]; then
echo $(date +%T%n%F) "Nginx is not running. Starting it..."
#/etc/init.d/nginx start
systemctl start nginx
else
echo "Nginx is running. Checking if it's healthy..."
# 检测进程是否正常运行
RESULT=$(kill -0 "${PROCESS_ID}" 2>&1)
if [[ "${RESULT}" =~ "No such process" ]]; then
echo $(date +%T%n%F) "Nginx is not running. Restarting it..."
# 结束进程
kill -9 "${PROCESS_ID}"
# 启动进程
/etc/init.d/nginx start
systemctl start nginx
else
echo "Nginx is running normally."
fi
fi
2- 定时任务:
vim /etc/crontab
5分钟检测一次
*/5 * * * * root sh /mnt/data/zzy/scripts/monitor_nginx.sh >>/mnt/data/logs/monitor/nginxstatus.log 2>&1
cat /mnt/data/logs/monitor/nginxlogfile.log
案例二: 监控mysql
1- 脚本 cat monitor_mysql.sh
#!/bin/bash
PROCESS_NAME="mysqld"
# 使用pgrep命令来查找进程id
PROCESS_ID=$(pgrep "${PROCESS_NAME}")
if [ "${PROCESS_ID}" = "" ]; then
echo $(date +%T%n%F) "MySQL is not running. Starting it..."
#/etc/init.d/mysql start
systemctl start mysql
else
echo "MySQL is running. Checking if it's healthy..."
# 检测进程是否正常运行
RESULT=$(kill -0 "${PROCESS_ID}" 2>&1)
if [[ "${RESULT}" =~ "No such process" ]]; then
echo $(date +%T%n%F) "MySQL is not running. Restarting it..."
# 结束进程
kill -9 "${PROCESS_ID}"
# 启动进程
# /etc/init.d/mysql start
systemctl start mysql
else
echo "MySQL is running normally."
fi
fi
2- 定时任务:
vim /etc/crontab
*/5 * * * * root sh /mnt/data/zzy/scripts/monitor_mysql.sh >>/mnt/data/logs/monitor/mysqlstatus.log 2>&1
案例三: 监控docker
1-脚本
cat monitor_docker.sh
#!/bin/bash
PROCESS_NAME="docker"
# 使用pgrep命令来查找进程id
PROCESS_ID=$(pgrep "${PROCESS_NAME}")
if [ "${PROCESS_ID}" = "" ]; then
echo $(date +%T%n%F) "Docker is not running. Starting it..."
#/etc/init.d/mysql start
systemctl start docker
else
echo "Docker is running. Checking if it's healthy..."
# 检测进程是否正常运行
RESULT=$(kill -0 "${PROCESS_ID}" 2>&1)
if [[ "${RESULT}" =~ "No such process" ]]; then
echo $(date +%T%n%F) "Docker is not running. Restarting it..."
# 结束进程
kill -9 "${PROCESS_ID}"
# 启动进程
# /etc/init.d/mysql start
systemctl start docker
else
echo "Docker is running normally."
fi
fi
2- 定时任务
vim /etc/crontab
*/5 * * * * root sh /mnt/data/zzy/scripts/monitor_docker.sh >>/mnt/data/logs/monitor/dockerstatus.log 2>&1
案例四: nacos(不太一样)
cat monitor_nacos.sh
#!/bin/bash
PROCESS_NAME="nacos"
# 使用pgrep命令来查找进程id
#PROCESS_ID=$(pgrep "${PROCESS_NAME}")
PROCESS_ID=$(ps -ef | grep nacos.nacos | grep -v grep | awk '{print $2}')
if [ "${PROCESS_ID}" = "" ]; then
echo $(date +%T%n%F) " $PROCESS_NAME is not running. Starting it..."
#/etc/init.d/nginx start
/mnt/data/nacos/nacos/bin/startup.sh -m standalone
else
echo "$PROCESS_NAME is running. Checking if it's healthy..."
# 检测进程是否正常运行
RESULT=$(kill -0 "${PROCESS_ID}" 2>&1)
if [[ "${RESULT}" =~ "No such process" ]]; then
echo $(date +%T%n%F) "$PROCESS_NAME is not running. Restarting it..."
# 结束进程
kill -9 "${PROCESS_ID}"
# 启动进程
/mnt/data/nacos/nacos/bin/startup.sh -m standalone
else
echo "$PROCESS_NAME is running normally."
fi
fi