The script below is an update to the workaround provided on page 6. It is a single-script installer for a watchdog launchdaemon that will probe your ethernet interface en0 every 45 seconds and restart it if the network gateway can't be pinged through this interface. This will recover connectivity if the ethernet interface should freeze. An uninstaller utility will be automatically created.
To deploy:
1. Open Terminal (Applications - Utilities - Terminal.app)
2. Select and copy the content of the first script below
3. Enter the following command in Terminal (you can copy and paste from here):
cat > install_en0_watchdog_service.sh
4. Paste in the terminal window the just copied script
5. Press control-D (press and hold the key control, then briefly press D and release both keys)
6. Enter the following command in Terminal (you can copy and paste from here):
chmod 755 install_en0_watchdog_service.sh
7. Execute the installation script by entering the following command in Terminal (you can copy and paste from here):
./install_en0_watchdog_service.sh
You will most likely be prompted for your user password (once every 10 minutes per each Terminal session); this is the sudo command which is required to elevate permissions to be able to install the launchdaemon with system permissions.
8. You are done!
How to know if it works:
- A directory named bin is created under your user profile folder (if it wasn't there already)
- A new script called restart_en0.sh will appear under this directory, as will an uninstallation utility named uninstall_en0_watchdog_service.sh.
- In the system's /tmp directory a file named restart_en0.log will be present and timestamped to the last time the watchdog was automatically run
- A file named restarted_en0.log will appear at that same location if the ethernet interface had to be restarted, with its timestamp indicating when that event had occurred.
How to uninstall:
1. Open the Terminal app
2. Run the uninstall utility by entering the command (you can copy and paste from here):
bin/uninstall_en0_watchdog_service.sh
You will most likely be prompted for your user password (once every 10 minutes per each Terminal session); this is the sudo command which is required to elevate permissions to be able to uninstall a launchdaemon that has system permissions.
3. Delete the leftover install and uninstall scripts from Finder.
4. You are done!
Begin selection with the # symbol below:
#!/bin/bash
#### creating watchdog script
script="/Users/`whoami`/bin/restart_en0.sh"
mkdir "/Users/`whoami`/bin" 2> /dev/null
sudo chown `whoami` $script 2> /dev/null
cat <<TAG2 > $script
#!/bin/bash
(
gw=\`/sbin/route -n get 8.8.4.4 \\
| /usr/bin/grep gateway \\
| /usr/bin/awk '{print \$2}'\`
/sbin/ping -c 1 -t 5 -b en0 \$gw \\
&& /bin/echo en0 alive \\
|| (
/sbin/ifconfig en0 down && \\
/sbin/ifconfig en0 up && \\
echo restarted > /tmp/restarted_en0.log
)
) &> /tmp/restart_en0.log
TAG2
chmod 755 $script
sudo chown root:wheel $script
### creating launchdaemon that will run watchdog script every 45 seconds
cat <<TAG1 > com.example.restart_en0.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.restart_en0</string>
<key>ProgramArguments</key>
<array>
<string>$script</string>
</array>
<key>StartInterval</key>
<integer>45</integer>
</dict>
</plist>
TAG1
sudo mv com.example.restart_en0.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/com.example.restart_en0.plist
### creating uninstall script
script="uninstall_en0_watchdog_service.sh"
cat <<TAG3 > $script
#!/bin/bash
### unloading launchdaemon
sudo launchctl unload /Library/LaunchDaemons/com.example.restart_en0.plist
### deleting launchdaemon
sudo rm /Library/LaunchDaemons/com.example.restart_en0.plist
### deleting watchdog script
sudo rm /Users/`whoami`/bin/restart_en0.sh
TAG3
chmod 755 $script
sudo chown `whoami` $script 2> /dev/null
### (re)starting launchdaemon that was just created
[ -e "/Library/LaunchDaemons/com.example.restart_en0.plist" ] && \
sudo launchctl unload /Library/LaunchDaemons/com.example.restart_en0.plist
sudo launchctl load /Library/Launchdaemons/com.example.restart_en0.plist
####stop selection above this line