I wonder how can I make the machine do something each time it connects to internet ?
like checking for updates once I'm connected to internet ?
Thanks
|
I wonder how can I make the machine do something each time it connects to internet ? like checking for updates once I'm connected to internet ? Thanks |
||||
|
|
|
There is absolutely no indicator that shows whether you have an internet connection or not. That is because you always connect to some kind of gateway (router, modem, ...) that sets up the connection to your ISP. So, to be sure whether you are online or not, you have to check the connection yourself by pinging or building up an HTTP connection (using You can be sure to have an internet connection if you know the expected response. For example, you can parse websites like http://www.whatismyip.de/ for an IP address using regular expressions. If your regular expression is correct and it doesn't find an IP address then you have no internet connection. Short answer: Write a script in which you check the internet connection (like described above). On success you execute the commands you want and set a flag that you are done for today (just create file and check for existance), on failure you do nothing. Make that script a cronjob. |
|||||
|
|
The script you are looking for looks like this
To easily setup a cronjob to run it every x minutes, you can install "gnome-schedule". |
|||
|
|
|
Network-manager exposes a DBus interface that you can use to be notified when you connect to a network. This will usually mean you're connected to the internet as well, but I don't know if Network-Manager has any methods for actually confirming that. You might want to use Network-Manager to get notified when you connect to a network, then confirm that you have internet access and if so, do what you want. |
|||
|
|
|
As dAnjou said, I don't believe there is a trigger you can use. However, to check for an Internet connection, there is an easier way than using curl. Use fping as follows, which returns 0 if connected and 2 if not. See fping's manual for more information.
(Using example.com is valid, because the domain name exists even though it is reserved for testing.) Simplifying Davidc's example becomes:
|
|||
|
|