Wpa_cli action file as service to run script when wlan0 connected

DietPi v10.4.2

I have a script in /etc which gets 2 arguments, interface (wlan0) and event (CONNECTED) from wpa_cli

I can manually call the script
wpa_cli -i wlan0 -a /etc/wlan0_connected.sh -B
and it works, it calls my script, passes the 2 args, and the script does it’s thing (writes to log files)…

Now, how do I create a service, to start the wpa_cli cmd on startup, so when the rpiz2w is rebooted it will be monitoring the wlan0 interface and logging the connected disconnect events?

i tried creating a service, but status shows:

RPIZ2W_wlan0_up.service: Deactivated successfully.

changed Type=simple to forking, that did not stop it from being deactivated…

thanks

Can you share the whole service file please

Inside a script, you need to call wpa_cli with its full path. Not just the command itself.

service file

[Unit]
Description=Daemon to receive the wpa_supplicant event
[Service]
#Type=simple
Type=forking
# call wpa_cli pass as arg -a for action script, path to script, interface wlan0, run in Background
ExecStart=/usr/sbin/wpa_cli -a /etc/wpa_ntfy.sh -i wlan0 -B
[Install]
WantedBy=multi-user.target

thanks, but my script is not calling wpa_cli, the wpa_cli is calling my script using the -a (action) arg.

the service file is calling the wpa_cli, and it is using the full path to it

here is an excerpt from journalctl | grep wpa

it shows the wifi disconnecting at 2 am and 8:30, but my script defined by action is not called. i hope this helps.

Jun 06 02:38:26 DietPi wpa_supplicant[1074]: wlan0: CTRL-EVENT-DISCONNECTED bssid=94:a6:7e:de:6b:f1 reason=3 locally_generated=1
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=US
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: p2p-dev-wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: p2p-dev-wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: nl80211: deinit ifname=p2p-dev-wlan0 disabled_11b_rates=0
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: p2p-dev-wlan0: CTRL-EVENT-TERMINATING
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: nl80211: deinit ifname=wlan0 disabled_11b_rates=0
Jun 06 02:38:27 DietPi wpa_supplicant[1074]: wlan0: CTRL-EVENT-TERMINATING
Jun 06 02:38:28 DietPi wpa_supplicant[16694]: Successfully initialized wpa_supplicant
Jun 06 02:38:28 DietPi wpa_supplicant[16694]: nl80211: kernel reports: Registration to specific type not supported
Jun 06 02:38:29 DietPi wpa_supplicant[16697]: wlan0: Trying to associate with 92:fd:1e:86:9f:72 (SSID='mySSID' freq=2422 MHz)
Jun 06 02:38:29 DietPi wpa_supplicant[16697]: wlan0: Associated with 92:fd:1e:86:9f:72
Jun 06 02:38:29 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
Jun 06 02:38:29 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=US
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-DISCONNECTED bssid=92:fd:1e:86:9f:72 reason=15
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: BSSID 92:fd:1e:86:9f:72 ignore list count incremented to 2, ignoring for 10 seconds
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=US
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: Trying to associate with 94:a6:7e:de:6b:f1 (SSID='mySSID-basement' freq=2462 MHz)
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: Associated with 94:a6:7e:de:6b:f1
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=US
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: WPA: Key negotiation completed with 94:a6:7e:de:6b:f1 [PTK=CCMP GTK=CCMP]
Jun 06 02:38:32 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-CONNECTED - Connection to 94:a6:7e:de:6b:f1 completed [id=0 id_str=]
Jun 06 08:27:05 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-DISCONNECTED bssid=94:a6:7e:de:6b:f1 reason=3 locally_generated=1
Jun 06 08:27:05 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jun 06 08:27:05 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=US
Jun 06 08:27:05 DietPi wpa_supplicant[16697]: p2p-dev-wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: p2p-dev-wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: nl80211: deinit ifname=p2p-dev-wlan0 disabled_11b_rates=0
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: p2p-dev-wlan0: CTRL-EVENT-TERMINATING
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: nl80211: deinit ifname=wlan0 disabled_11b_rates=0
Jun 06 08:27:06 DietPi wpa_supplicant[16697]: wlan0: CTRL-EVENT-TERMINATING
Jun 06 08:27:07 DietPi wpa_supplicant[27413]: Successfully initialized wpa_supplicant
Jun 06 08:27:07 DietPi wpa_supplicant[27413]: nl80211: kernel reports: Registration to specific type not supported
Jun 06 08:27:07 DietPi wpa_supplicant[27417]: wlan0: Trying to associate with 92:fd:1e:86:9f:72 (SSID='mySSID' freq=2422 MHz)
Jun 06 08:27:07 DietPi wpa_supplicant[27417]: wlan0: Associated with 92:fd:1e:86:9f:72
Jun 06 08:27:07 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
Jun 06 08:27:07 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=US
Jun 06 08:27:07 DietPi wpa_supplicant[27417]: wlan0: WPA: Key negotiation completed with 92:fd:1e:86:9f:72 [PTK=CCMP GTK=CCMP]
Jun 06 08:27:07 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-CONNECTED - Connection to 92:fd:1e:86:9f:72 completed [id=1 id_str=]
Jun 06 08:30:36 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-DISCONNECTED bssid=92:fd:1e:86:9f:72 reason=3 locally_generated=1
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=CORE type=WORLD
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=US
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: p2p-dev-wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: p2p-dev-wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: nl80211: deinit ifname=p2p-dev-wlan0 disabled_11b_rates=0
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: p2p-dev-wlan0: CTRL-EVENT-TERMINATING
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-DSCP-POLICY clear_all
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: nl80211: deinit ifname=wlan0 disabled_11b_rates=0
Jun 06 08:30:37 DietPi wpa_supplicant[27417]: wlan0: CTRL-EVENT-TERMINATING
Jun 06 08:30:38 DietPi wpa_supplicant[27602]: Successfully initialized wpa_supplicant
Jun 06 08:30:38 DietPi wpa_supplicant[27602]: nl80211: kernel reports: Registration to specific type not supported
Jun 06 08:30:38 DietPi wpa_supplicant[27605]: wlan0: Trying to associate with 94:a6:7e:de:6b:f1 (SSID='mySSID-basement' freq=2462 MHz)
Jun 06 08:30:38 DietPi wpa_supplicant[27605]: wlan0: Associated with 94:a6:7e:de:6b:f1
Jun 06 08:30:38 DietPi wpa_supplicant[27605]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
Jun 06 08:30:38 DietPi wpa_supplicant[27605]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=US
Jun 06 08:30:39 DietPi wpa_supplicant[27605]: wlan0: WPA: Key negotiation completed with 94:a6:7e:de:6b:f1 [PTK=CCMP GTK=CCMP]
Jun 06 08:30:39 DietPi wpa_supplicant[27605]: wlan0: CTRL-EVENT-CONNECTED - Connection to 94:a6:7e:de:6b:f1 completed [id=0 id_str=]

i will add, my router has a script which sends me a NTFY when a wireless device connects, so that allows me to see when the rpi is re-joining the wifi, and i can correlate the timestamps to the log on the rpi.

This is expected, it starts the CLI ones as a daemon and then the service deactivates itself, but the CLI is still running in the background.

If you want it to run in foreground (so the service stays active) you need to remove -B flag.

Here is a more robust service

[Unit]
Description=wpa_cli monitor for wlan0
After=network.target wpa_supplicant.service
Requires=wpa_supplicant.service

[Service]
Type=simple
ExecStart=/sbin/wpa_cli -i wlan0 -a /etc/wpa_ntfy.sh
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target

thank you, I tried your service file and rebooted

but journalctl | grep wpa, shows dependency failed errors

Jun 06 10:39:37 DietPi systemd[1]: Dependency failed for wpa_supplicant.service - WPA supplicant.
Jun 06 10:39:37 DietPi systemd[1]: Dependency failed for RPIZ2W_wlan0_up.service - wpa_cli monitor for wlan0.
Jun 06 10:39:37 DietPi systemd[1]: wpa_supplicant.service: Job wpa_supplicant.service/start failed with result 'dependency'.
Jun 06 10:39:38 DietPi wpa_supplicant[347]: Successfully initialized wpa_supplicant
Jun 06 10:39:38 DietPi wpa_supplicant[347]: nl80211: kernel reports: Registration to specific type not supported
Jun 06 10:39:38 DietPi wpa_supplicant[383]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=USER type=COUNTRY alpha2=US
Jun 06 10:39:39 DietPi wpa_supplicant[383]: wlan0: Trying to associate with 16:8d:2d:18:02:b5 (SSID='mySSID' freq=2422 MHz)
Jun 06 10:39:39 DietPi wpa_supplicant[383]: wlan0: Associated with 16:8d:2d:18:02:b5
Jun 06 10:39:39 DietPi wpa_supplicant[383]: wlan0: CTRL-EVENT-SUBNET-STATUS-UPDATE status=0
Jun 06 10:39:39 DietPi wpa_supplicant[383]: wlan0: CTRL-EVENT-REGDOM-CHANGE init=COUNTRY_IE type=COUNTRY alpha2=US
Jun 06 10:39:39 DietPi wpa_supplicant[383]: wlan0: WPA: Key negotiation completed with 16:8d:2d:18:02:b5 [PTK=CCMP GTK=CCMP]
Jun 06 10:39:39 DietPi wpa_supplicant[383]: wlan0: CTRL-EVENT-CONNECTED - Connection to 16:8d:2d:18:02:b5 completed [id=1 id_str=]

i found the service for wpa_supplicant and enabled it, rebooted, checked status

sudo systemctl status wpa_supplicant.service
○ wpa_supplicant.service - WPA supplicant
     Loaded: loaded (/usr/lib/systemd/system/wpa_supplicant.service; enabled; pr                                          eset: enabled)
     Active: inactive (dead)

Jun 06 13:45:17 DietPi systemd[1]: Dependency failed for wpa_supplicant.service                                           - WPA supplicant.
Jun 06 13:45:17 DietPi systemd[1]: wpa_supplicant.service: Job wpa_supplicant.se                                          rvice/start failed with result 'dependency'.

If you didn’t use wpa_supplicant service before then you can just remove it from the requirements and deactivate it, as it was before

hello again,

I gave up on trying to get the wpa_sup service to work as I needed it, and searhced for other methods to achieve what I wanted. I found mention of “NetworkManger”, but that is not installed in DietPi. Then I learned about ifupdown, which they claim is old, but it is still used by Debian, and since DietPi is Debian, I found it did exactly what I wanted.

When my wifi interface is brought up (especially after a wi-fi re-connect due to bad location), any scripts located in /etc/network/if-up.d are run

#!/bin/sh
# 2026-06-07 5:53 pm
# whenever a network interface is brought up, i.e. it is active (including when wifi re-connects after being dropped)
# any scripts in the folder /etc/network/if-up.d
# will be run
# make sure the script file is executable chmod +x scriptname
# this script ensures the interface is wlan0 (the main wifi)
# and then does stuff, ie write to log files
# environment variables are available, i found them using google search

# Only run if the interface being brought up is wlan0
if [ "$IFACE" = "wlan0" ]; then
    # Place your custom commands below this line
    echo "wlan0 is up at $(date)" >> /var/log/wlan0_custom.log
    echo "mode is $MODE" >> /var/log/wlan0_custom.log
    echo "addrfam is $ADDRFAM" >> /var/log/wlan0_custom.log
    echo "logical is $LOGICAL" >> /var/log/wlan0_custom.log
    echo "method is $METHOD" >> /var/log/wlan0_custom.log
    echo "phase is $PHASE" >> /var/log/wlan0_custom.log
    MY_HOSTNAME=$(hostname)
    echo "hostname is $MY_HOSTNAME" >> /var/log/wlan0_custom.log
    MAC_ADDR=$(ip link show dev "$IFACE" | awk '/ether/ {print $2}')
    echo "maccaddr is $MAC_ADDR" >> /var/log/wlan0_custom.log
    # Extract SSID
    SSID=$(iw dev wlan0 link | grep "SSID:" | awk -F': ' '{print $2}')
    echo "SSID is $SSID" >> /var/log/wlan0_custom.log
    # Extract Signal Strength in dBm
    SIGNAL=$(iw dev wlan0 link | grep "signal:" | awk -F': ' '{print $2}' | xargs)
    echo "ssid signal dBm is $SIGNAL" >> /var/log/wlan0_custom.log


fi