Script To Restart Internet | Mac
I’ve recently been having trouble with my MacMini Server disconnecting from Airport and not automatically 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.
submitted by /u/desepticon
0