注意:此脚本假设您使用的是基于systemd的系统(例如Ubuntu或CentOS),如果您使用的是不同的init系统,则可能需要相应地修改脚本。
while true
do
ssh_status=$(systemctl status sshd | grep "active (running)")
if [ -z "$ssh_status" ]; then
echo "SSH service is down, restarting..."
systemctl restart sshd
fi
sleep 20
done
您可以将此脚本保存到一个文件(例如,ssh_monitor.sh),用chmod +x ssh_monitor.sh。使其可执行,然后用nohup ./ssh_monitor.sh &在后台运行。