user1149499
user1149499

Reputation: 583

Cron job to restart network ONLY if it's down?

I've had frustrating issues with a computer whose network drops at apparently random times and as a work-around I made a simple cron job to restart it every hour. But this seems silly if the network is already up -- why restart it, right?

So is there a simple script I could create that will first check (ping? ifconfig?) to see if the network is working and then only restart if that returns false? Or a flag on network-manager that will check itself?

My cron is just this:

15 * * * * /sbin/service network-manager restart

Thanks in advance to anyone with ideas on how best to "network-manage" this! (pun intended!) :-)

Upvotes: 0

Views: 5874

Answers (1)

Dave X
Dave X

Reputation: 5137

Call a more complicated script from your crontab:

15 * * * * mySafeRestart


#!/bin/bash  
# mySafeRestart : restart only if uncommunicative
if ! ping -c 1 remote.host.com ; then 
    /sbin/service network-manager restart
fi

As for a test for network up, see How to test an Internet connection with bash?

Using the netcat test suggested below would be:

if ! nc -w 2 google.com 80 ; then
  ...

Upvotes: 1

Related Questions