MacOS – Turning Off Wi-Fi During Logout Script Doesn’t Work With Launchd

automationlaunchdmacosscript

I'm writing a launchd plist and a shell script on my MacBook running Yosemite to automate the following scenario:

Turning off Wi-Fi when the user logs out.

Here's my script:

#!/bin/sh
onLogout() {

    #Turning off Wi-Fi. Tested in the Terminal and worked.
    /usr/sbin/networksetup -setairportpower en0 off

    #Log a message as a proof that the script is executed
    echo 'Logging out' >> ~/Desktop/logout.sh.log
    exit
}

trap 'onLogout' SIGINT SIGHUP SIGTERM
while true; do
    sleep 86400 &
    wait $!
done

When I logged out, I did see the log message appearing in the log file. But the Wi-Fi was still on.

Please help.

Best Answer

Use networksetup -listallhardwareports to get a list of all network devices. Usually en0 is the ethernet interface, en1 is WiFi.