Linux Shell restarts the network card if ping fails (detailed explanation)

direct script

--------------------------------------------------------------------------

#vi  /tmp/ping_check.sh

#!/bin/bash

IP="1.1.1.1"

PacketLoss=`ping -c 4 -w 4 1.1.1.1 | grep 'packet loss' | awk -F 'packet loss' '{print $1}' | awk '{print $NF}'|sed 's/\%//g'`

if  [$Packetloss -eq 100]

then

echo "ping failed `date`" >>/tmp/ping_check_failed.log

systemctl restart network

#chmod +x ping_check.sh Grant execution permissions

#crontab -e Add scheduled tasks

*/20 * * * * /bin/sh /tmp/ping_check.sh Runs a script every 20 minutes

----------------------------------------------------------------------------------

Guess you like

Origin blog.csdn.net/sj349781478/article/details/132360286