Я искал сценарий, который будет постоянно проверять мое подключение к Интернету, которое будет запускаться всякий раз, когда мой сервер будет включен, и я сделал это:
Сначала установите fpingapt-get install fping
Создайте сценарий инициализации в в папке /etc/init.d со следующим содержимым (я назвал его testcon) #!/bin/bash
PIDFILE=/var/run/testcon.pid
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting internet connection tester"
/etc/init.d/testcond &
echo $! > $PIDFILE
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping internet connection tester"
PID=$(cat $PIDFILE)
kill -9 "$PID"
log_end_msg $?
;;
*)
echo "Usage: /etc/init.d/testcon {start|stop}"
exit 1
;;
esac
exit 0
Создайте сценарий в папке /etc/init.d со следующим содержимым (я назвал его testcond) #echo Computer starting or testing daemon init
while [ "$itest" == "" ]
do
#wait 5 seconds
sleep 5
itest=$(fping 8.8.8.8 | grep alive)
done
date | mail -s "Server is up and Internet is online" your_email@gmail.com
#loop forever
while [ "1" == "1" ]
do
itest=$(fping 8.8.8.8 | grep alive)
#if internet is down
if [ "$itest" == "" ]
then
#echo Internet is down
#log time it was found down
current_time=$(date)
echo "Internet was found down @ $current_time" >> /mnt/data/Server/internet_log.txt
#loop until it is back
while [ "$itest" == "" ]
do
#wait 60 seconds
sleep 60
itest=$(fping 8.8.8.8 | grep alive) # test the internet
done
#when it is back
current_time=$(date)
echo "Internet is back @ $current_time" >> /mnt/data/Server/internet_log.txt
body=$(tail -2 /mnt/data/Server/internet_log.txt)
echo "$body" | mail -s "Internet is back online" your_email@gmail.com
fi
#echo Internet is online
#wait 60 seconds
sleep 60
done
Затем я запускаю команды ниже, чтобы добавить к запуску: sudo update-rc.d testcon defaults
sudo update-rc.d testcon enable
Перезагрузка