1

I have a Raspberry Pi that I use as a NAS for my local network. It works fine, but when I turn off the router to which the pi is connected, it permanently loses the connection. To be able to connect to the network again it has to be restarted. Therefore I have written this script:

#!/bin/bash

if ! ping -q -w 1 -c 1 $(ip r | grep default | cut -d ' ' -f 3)
then
        service networking restart

        sleep 60

        if ! ping -q -w 1 -c 1 $(ip r | grep default | cut -d ' ' -f 3)
        then
                reboot
        fi
fi

It basically tries to ping the router. If it fails it restarts the networking service (for when a simple error occurred and a reboot is not necessary). If it then still can't ping to the router it reboots. I set this up as a cronjob every 10 minutes, but it does not work. What am I doing wrong?

I put this line in the crontab:

0,10,20,30,40,50 * * * * /root/rebooter.sh >> rebooter.log 2>&1

Whit no connection this is the output:

Usage: ping [-LRUbdfnqrvVaAD] [-c count] [-i interval] [-w deadline]
            [-p pattern] [-s packetsize] [-t ttl] [-I interface]
            [-M pmtudisc-hint] [-m mark] [-S sndbuf]
            [-T tstamp-options] [-Q tos] [hop1 ...] destination
/root/rebooter.sh: Line 5: service: Command not found.
Usage: ping [-LRUbdfnqrvVaAD] [-c count] [-i interval] [-w deadline]
            [-p pattern] [-s packetsize] [-t ttl] [-I interface]
            [-M pmtudisc-hint] [-m mark] [-S sndbuf]
            [-T tstamp-options] [-Q tos] [hop1 ...] destination
/root/rebooter.sh: Line 11: reboot: Command not found.

With connection this is the output:

PING 192.168.178.1 (192.168.178.1) 56(84) bytes of data.

--- 192.168.178.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.985/0.985/0.985/0.000 ms
  • can you try to add @reboot </abs/path/to script> & to your crontab? and reboot? – Simply_Me Aug 18 '14 at 16:54
  • Any error messages in rebooter.log ? – Mark Plotnick Aug 18 '14 at 17:07
  • @Simply_Me What does that do? – BrainStone Aug 18 '14 at 17:23
  • @BrainStone this to let cron know to run this script at reboot with & for not to wait for script complete. – Simply_Me Aug 18 '14 at 17:25
  • 1
    So it's not finding ip, reboot, or service. Looks like you need to set PATH in the shell script, since cron does not read any shell dot files that will set PATH. Edit that shell script so export PATH=/usr/sbin:/usr/bin:/sbin:/bin is the 2nd line. – Mark Plotnick Aug 18 '14 at 17:46
1

I think the issue is with your if statement.

if ! ping -q -w 1 -c 1 $(ip r | grep default | cut -d ' ' -f 3)

More specifically:

ping -q -w 1 -c 1 $(ip r | grep default | cut -d ' ' -f 3)

The host ip isn't passed properly.

Note that each portion works by itself (i.e. ping host and ip r....).

This has been working for me:

$ ip r | grep "default" | cut -d ' ' -3 | xargs ping -q -w 1 -c 1

Try to incorporate it with your if statements. xargs takes the output from previous pipes and use it for ping, whereas previously the ip wasn't passed to the ping half of the code. Try it in command line :-)

Changed to netstat rather then ip r which also works with Pi yet is general enough to be used across other Linux dists.

The script below:

It properly gets the host name to ping and enters the statement(s) in case of a 0 (input, i.e. when the device is disconnected).

Also re structured the if statement with [] brackets, and assigned a variable for cleaner statement.

#!/bin/bash

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" ] ;
then
    echo "service networking restart"

    sleep 60
    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" ] ;
    then
            echo "reboot"
    fi
fi
  • I don't think this won't make any difference since when the Pi is not connected the ip r | grep default | cut -d ' ' -f 3 command has an empty output. – BrainStone Aug 18 '14 at 18:22
  • @BrainStone but earlier it would have always been passed as empty (i.e. always in "not connected state"). – Simply_Me Aug 18 '14 at 18:25
  • @BrainStone check updated answer, this script should work for you. – Simply_Me Aug 18 '14 at 18:45
  • why did you put the commands in an echo? – BrainStone Aug 18 '14 at 20:59
  • @BrainStone because it was about fixing the statements, and not about reboot .... didn't feel like rebooting my system :-). – Simply_Me Aug 18 '14 at 21:00

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.