I am a newbie with Raspberry Pi. I am having one Pi3 and one Pi Zero. Pi3 is acting as a router and my PiZero is connected with it.

I want to trigger a script on connect-disconnect event. I have configured the SSID and password in the wpa_supplicant.conf file. How can I ensure that my script is triggered when WiFi connection is established/broken?

Any help will be appreciated.


You may write a wpa_cli action script.e,g:


case "$2" in
        notify-send "WPA supplicant: connection established";
        notify-send "WPA supplicant: connection lost";

use wpa_cli -a /path/to_your/script.sh (make it executable) to perform action on connect.

If you need more custom stuff you can put a script in crontab. For example to keep it simple you may determine online/offline status by pinging wlan gateway...

|improve this answer|||||
  • used this command wpa_cli -a /home/pi/run_on_connect.sh (made it executable). but the output is showing selected interface 'p2p-dev-wlan0' and then blank. I wrote a script something like wlan='/sbin/ifconfig wlan0 |grep inet | wc -1' if [ $wlan -eq 0 ];then echo NOT CONNECTED else echo CONNECTED – naruto uzumaki Jan 31 at 11:23
  • If you run manually the script expected output is correct? There are some syntax errors to me. – realpclaudio Jan 31 at 11:32
  • yeah, I ran the script manually and the output is correct. wpa_cli -a command seems something wrong. – naruto uzumaki Jan 31 at 11:43
  • 2
    Take a look here: Arch documentation but should fit wiki.archlinux.org/index.php/WPA_supplicant It's a daemon running, so put it in the background to test and use its builtin variables to determine connection/disconnection events. – realpclaudio Jan 31 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.