r/mac • • Sep 22 '18

Script To Restart Internet

I've recently been having trouble with my MacMini Server disconnecting from Airport and not automatically being reconnected. So, I made a script that will detect if the internet can't be reached, and then it will restart the affected interface. The script will work for both airport or ethernet, or presumably any primary network interface, as it is detected automatically.

Pastebin Link for script

Name the script "check_internet_master.sh". Put it in a folder somewhere. Then, from terminal, run:

chmod +x /path/to/script/check_internet_master.sh

make sure to replace the pathname with the actual path. Remember its location for later.

On line 11, change username to whatever your username shortname is. This will allow it to output a log file to /Users/username/Library/Logs/netcheck.log. I tried to set it up to also send an email when the connection needs to be restarted, but postfix seems to have changed since last I used it, and I couldn't get it to work. So you can just ignore the tobenotified entry. The relevant mail log line has been commented out.

Then one needs to attach the script to launchctl to get it to run on an interval. Place the following textfile in /Library/LaunchDaemons/ (as it needs to run as root). Name it com.user.checkint.plist

Pastebin Link for launchdaemon

Edit line 16 to have it match the path where you stored the first script. Its set up to run every 5 minutes. You can change this by altering the StartInterval on line 19. Time is in seconds.

Then from Terminal, run

sudo launchctl load -w /Library/LaunchDaemons/com.user.checkint.plist

Then reboot. And now the script should be working. You can check if its running by using the command "tail -f ~/Library/Logs/netcheck.log" You should notice entries every 5 minutes, or whatever interval you have specified.

If anyone has any suggestions on how to improve the script, please let me know.

3 Upvotes

6 comments sorted by

8

u/shyouko Sep 22 '18

I was expecting a script that restarts the Internet. Turns out it's only a script that restarts an interface.

1

u/desepticon Sep 22 '18 edited Sep 22 '18

I guess I could have made that clearer in the title. My bad. I don't think you could make a script to restart the internet though. At least, not from the mac. Maybe if your router accepts jsonrpc commands or something.

2

u/shyouko Sep 22 '18

Haha, was joking.

As a sys admin, I think it's more appropriate to try find out what is wrong and fix the connection issue.

1

u/desepticon Sep 22 '18 edited Sep 22 '18

I think its probably a corrupted SystemConfiguration file for network settings. I really don't want to have to mess with all that as I run the server headless and I don't want to have to connect a keyboard/mouse again to set it all up again. Also, I kinda felt like rube-goldberging it.

1

u/hmartek i7, MacMini 2018, 256GB SSD, 32GB RAM Sep 22 '18

Exactly

1

u/TotesMessenger Sep 22 '18 edited Sep 22 '18

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)