Linux 服务监控脚本
个人搭建的zabbix服务器因性能问题总是关闭,所以写了个脚本对zabbix-server服务进行监控。当服务停止时,脚本会自动拉起服务并记录日志,方面定位排查。
[root@elttwl ~]# vim /opt/scripts/zabbix-monitor.sh
#!/bin/bash
SERVICE=zabbix-server
while true
do
systemctl status $SERVICE > /dev/null
if [ $? -gt 0 ]; then
DATE=`date "+%Y-%m-%d %H:%M:%S"`
STATE="service is not running."
echo $STATE
echo $DATE $STATE >> /var/log/zabbix-monitor/zabbix-monitor.log
systemctl start $SERVICE
echo "service is started."
else
echo "service is running."
fi
sleep 60
done
通过创建Systemd服务是脚本长期稳定运行
[root@elttwl ~]# vim /etc/systemd/system/zabbix-monitor.service
[Unit]
Description=Zabbix-server service monitor
After=network.target
[Service]
Type=simple
User=root
# 监控脚本的路径
ExecStart=/bin/bash /opt/scripts/zabbix-monitor.sh
Restart=on-failure
RestartSec=5
[Install]
WantedBy=mutil-user.target
执行以下命令使Systemd重新加载配置文件
systemctl daemon-reload
启动服务并设置开机自启
# systemd文件的名字即为服务的名字
systemctl start zabbix-monitor.service
systemctl enable zabbix-monitor.service
标签:脚本,服务,monitor,service,zabbix,systemctl,监控,Linux
From: https://www.cnblogs.com/elttwl/p/18079905