Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was just wondering if it was possible to run a script as soon as a test to see if a network interface is up (like a ping or something)? I think it would be pretty awesome to update my yum when I login, but I want to make sure that I have internet access first. Any ideas?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Why not, instead of whenever you log in, just set up a cron job to do it every morning (say 3am when it wouldn't effect anyone (assuming your machine is up 24/7))?

If you really wanted to do it every time you logged in you could just put yumupdate.sh in your .bash_login

Alternatively in your crontab:

00 03 * * * yumupdate.sh

And in yumupdate.sh

#!/bin/bash            
while sleep 300; do     # sleep 5 minutes in between each ping test
    nc -vz 8.8.8.8 53   # nc to test connectivity (8.8.8.8 is google dns)
    if [ $? -eq 0 ]     # if the previous exit code == 0 (no error)
        then            # update yum then update the os then break out of the loop
            /usr/bin/yum -y update yum >> ~/yumupdateyum.log 
            /usr/bin/yum -y update >> ~/yumupdate.log
            exit
    fi
done
share|improve this answer
    
fwiw since this is a laptop, it's probably better to do a netcat TCP test to google's DNS to test for internet connectivity so you give out fewer false negatives for networks that filter pings: nc -vz 8.8.8.8 53 –  Joel Davis Apr 8 '13 at 18:47
    
I'll also point out that it's probably better to not do a loop since cron will perform that function for you if you modify the time spec you put on it and how you have it right now could result in several instances of yumupdate.sh getting spun up if they're without network connectivity for a while. Also by not suppressing stdout for PING root will get an email every time this script is ran. –  Joel Davis Apr 8 '13 at 18:49
    
@JoelDavis the OP never specified laptop/desktop/server. Unless my lack of caffeine is causing selective blindness :P –  h3rrmiller Apr 8 '13 at 18:50
    
Fair point, I guess I had assumed it was a laptop since it's fedora most other hardware configurations don't have intermittent network connectivity (outside of remote facilities, but that's a rare setup). Either way it's probably still good to offer nc over ping since we don't know their setup. –  Joel Davis Apr 8 '13 at 18:53
    
@JoelDavis agreed, added to the answer –  h3rrmiller Apr 8 '13 at 19:02
show 2 more comments

Maybe you want to look at:

yum install yum-NetworkManager-dispatcher

...and maybe add features to it?

share|improve this answer
1  
you might want to add an example of the script and how to use it –  midnightsteel Apr 10 '13 at 20:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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