1

So I have a server and a client computer which I must have running on the network all the time. Somemtimes because I am running my client computer on WLAN the network goes down so I need to restart it. Because I want to garantuee there is a active connection between my server and client I must have someway of sending a regular check with the client computer if it is essentialy alive or responsive.

My problem

I need to first reboot the network if the client doesn't receive any such connectivity ping from my server within five minutes and if the network has rebooted and it still doesn't receive any such packets from my server it will reboot the whole linux machine.

The idea is to have my server act like a watchdog which will send a connectivity ping to my client every minute and if the client doesn't receive any such pings within five minutes it will try to reinitialize itself.

What I tried

I tried with this local script to check if it can ping my server from my client but it wasn't working so I want to check from my server end instead.

#!/bin/bash

test_ping=`ping -c 4 SERVER_ADDR | tail -1| awk '{print $4}' | cut -d '/' -f 2`'>'1000 | bc -l
test_host=`netstat -nr | grep "UG" | awk '{ print $2}' | xargs ping -q -w 1 -c 1 | grep "received" | awk '{ print $4 }'`
if [ "$test_host" == "0" ] || [ -z "$test_host" ] || [ "$test_ping" == "1"] ;
then
    echo "restarting network at $(date '+%A %W %Y %X')" >> /path/to/my/logs.file
    service networking restart

    sleep 60
    test_ping=`ping -c 4 SERVER_ADDR | tail -1| awk '{print $4}' | cut -d '/' -f 2`'>'1000 | bc -l
    test_host=`netstat -nr | grep "UG" | awk '{ print $2}' | xargs ping -q -w 1 -c 1 | grep "received" | awk '{ print $4 }'`
    if [ "$test_host" == "0" ] || [ -z "$test_host" ] || [ "$test_ping" == "1"] ;
    then
        echo "rebooting at $(date '+%A %W %Y %X')" >> /path/to/my/logs.file
        reboot
    fi
fi

Any ideas how I can accomplish this in Linux?

0

I "solved" my problem by using ssh to create a temporary file on the client

ssh -o ConnectTimeout=5 USER@CLIENT_HOST '/usr/bin/touch /tmp/watchdog.hook' 

This was called by cron every minute on my server using the cron command below

*  *    * * *   /path/to/script/watchdog-server.sh

On the client side I try to delete the temporary file and if it fails, a counter will be incremented and if it is equal to three it will restart the network, and if is five it will reboot the machine. If it succeeds it will reset the counter.

counter_file="/tmp/watchdog.counter"
if [ ! -f "$counter_file" ]; then
    printf '0\n' >"$counter_file"
fi
counter_curr=$(< "$counter_file")
rm /tmp/watchdog.hook
if [ $? -eq 0 ]; then
    counter_curr=0
else
    (( ++counter_curr ))
    if [ "$counter_curr" -eq 3 ]; then
        echo "No network connection, restarting wlan0 at $(date)"
        /sbin/ifdown 'wlan0'
        sleep 5
        /sbin/ifup --force 'wlan0'
    elif [ "$counter_curr" -ge 5 ]; then
        echo "No network connection, rebooting machine at $(date)"
        /sbin/shutdown -r now
    fi
fi
printf '%s\n' "$counter_curr" >"$counter_file"

We want to wait 30 seconds before running the script on the client so we add this to cron:

*   * * *   * ( sleep 30 ; /path/to/script/watchdog-client.sh )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.