I have some anacron jobs which run daily. The scripts update local bzr and git repositories. Naturally these scripts need working network connections. I'm on a laptop and often wired and wireless internet do not come up fast enough. This causes my cron job to time out when pulling the repositories =(
So:
How to make sure the internet is up before running specific cron jobs? Or how to fail a job if there is no network, such that it is retried by anacron later again?
I think you can use Upstart to help you there. Mind you, I haven't tested that code below works but something very similar should.
That pretty much it. You might want to add some code to check that it does not run very often. You might also want to add
start update-repositories
to your crontab, it'll make sure your update will happen if you are on the net constantly for a prolonged period of time.I made a cron that did a ping test on a DNS server to ensure networking. Something like this:
Recently I've used something like this:
This isn't the MOST elegant - I'm not sure how else to check via a simple command or file on the system, but this has worked for me when needed.
You can talk to NetworkManager to see whether you are connected or not:
Just to wrap up a couple of the options here into a single script:
I plan to maintain this script at https://gist.github.com/JonTheNiceGuy/5cf4a23c8f2f755a9ca4
To expand on nixternal, the
fping
binary is excellent for that. You can cook it up in one-liners as inAs you see, yoo.mama does not like me but Google does. In crontab, you'd do something like
What I do is create a shell script that does what you need, ie. checks for network connection and then fires off the updates. Then call the script from cron.
I've defined a
bash
alias to answer this question:You could use it, or process the output of
ip link show
yourself.To handle "net goes down/net comes up" see my
net-o-matic
script at net-o-maticI found
ping
solutions gave me network errors which spammed my logs unless I laboriously directed errors to /dev/nullNetwork Manager can be used: