Disabling WiFi Power Management Permanently for Raspberry PI 3 with Raspbian Jessie

One problem I found when playing with my Raspberry PI3 (running Raspbian Jessie), is that the WiFi would drop after a certain amount of inactivity.  This was problematic at VMUG Meetings, and due to lack of having a screen to connect, I was forced to power-cycle the unit to get it back online.  After doing some googling, I found the built-in WiFi for the Raspberry PI 3 had some Power Management features, which were enabled by default.

You can check the status by running the follow command:

sudo iwconfig wlan

image

I found a simple command to disable the power management features, however the command only lasted until the next reboot and then went back to the default setting. The command is below

sudo iwconfig wlan0 power off

After doing some investigating, I came across this blog post on how to run a script at startup.  This enabled me to put the above command into a script and run it at startup!  http://www.raspberrypi-spy.co.uk/2015/10/how-to-autorun-a-python-script-on-boot-using-systemd/

The steps were simple.

1.  Make sure you’re in the /home/pi directory
image

2.  Run the command below to create a script :

sudo nano script.sh

image

3.  Enter the command above into the new script.sh file.  Don’t forget to include the first line though, or it won’t run properly.  You also don’t need SUDO before iwconfig here, as the script is being run at an elevated level already. 🙂

#!/bin/sh -
iwconfig wlan0 power off

CTRL-X then Y and Enter will save the file.
image

4.  Next, we have to create a configuration file (aka a unit file) that tells systemd what we want to do when…

sudo nano /lib/systemd/system/myscript.service

image

5.  Enter the text below into the text editor


[Unit]
Description=My Script Service
After=multi-user.target

[Service]
Type=idle
ExecStart=/home/pi/script.sh

[Install]
WantedBy=multi-user.target

 

6. The permissions on the unit file need to be set to 644.

sudo chmod 644 /lib/systemd/system/myscript.service

image
7.  Verify the permissions were set properly.

ls –l /lib/systemd/system/myscript*

image

 

8. Now that the unit file has been defined, we can tell systemd to start it during the boot sequence.


sudo systemctl daemon-reload

image

 

9. Next.

sudo systemctl enable myscript.service

image

 

10. Finally, REBOOT!

sudo reboot

image

 

11. Once your PI is back online, login and check the status now…

sudo iwconfig wlan0

2017-02-12 13_28_44-2017-02-12 13_18_32-pi@raspberrypi_ ~.png - Paint

 

Enjoy!

 

Ben Liebowitz, VCP, vExpert
NJ VMUG Leader

 

 

Share This:

24 thoughts on “Disabling WiFi Power Management Permanently for Raspberry PI 3 with Raspbian Jessie

  1. …that’ll work, but adding the ‘iwconfig wlan0 power off’ command to the /etc/rc.local script is a much simpler solution!

    1. @Antoine Megans – Thank You for the simple way.

      For me, this reduces excessive tx retries by factor of 10, and ping “Request timeout” by factor of 2 on Pi3 onboard WiFi.

    2. more simply is to specify “wireless-power off” in /etc/network/interfaces no annoying script and self documented setup, enjoy!
      iface wlan0 inet manual
      wireless-essid myessid
      wireless-power off
      wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

    3. How do i go on about this? sorry i dont know anything about anything about Pi. step by step would be nice, because the instructions above dont work. Step 7 ” ls –l /lib/systemd/system/myscript*” doesnt exists etc.

    1. In Raspian Stretch on my Pi3, adding “iwconfig wlan power off” to /etc/network/interfaces did not turn power management off. I had to add it to the /etc/rc.local to make it take effect.

  2. For script.sh, I needed to do a chmod+x to get this to work.
    /etc/network/interfaces was empty on a clean install of Raspian Stretch.-has it been deprecated in Stretch?

    New to Raspbian, but supposedly the point of doing it this way was to make sure that the system was in a known state before executing the script file
    Works great for setting up an raspberry pi 2 as an airplay receiver.

    1. This was written against a PI running a version of Raspbian from 2016. I can’t guarantee it will work with newer releases of the OS.

  3. Thanks for this, something I’ve found, and not just with your commands; when I copy them into putty ssh the ‘l’ (L) becomes a ‘1’ (one) and the command is rejected…

  4. If you have entered all the information and it does not seem to work..

    Try: “sudo systemctl status myscript.service” to output the status of your service.

    If the output says “203 Exec failure” you might need to add the following to the script.sh file (note the part after ‘ExecStart=’:

    [Service]
    Type=idle
    ExecStart=/bin/bash /home/pi/script.sh

    Hope this helps for people struggling with this!

  5. A much easier way, that doesn’t even require a script, is to use cron to run your command for you on boot. Since you want the command run as root, we will put it in the root crontab. We will be editing here, so be sure to set your EDITOR variable first if you don’t want nano — I’m an old UNIX guy so I use vi.

    # Edit root’s crontab
    $ sudo crontab -e -u root

    Add a line that looks like:

    @reboot iwconfig wlan0 power off

    Save the file and reboot. Cron will run the command as root each time your Pi boots.

  6. For some reason – my entry of the iwconfig command would not take effect in either /etc/rc.local or root’s crontab @reboot. I looked around among those initialization scripts in init.d and systemd but I didn’t see much of anything that could have led to persistent power: on state. So to keep it simple, I decided to just leave the command in the pi user’s .bashrc – this is one of the final steps after startup, so I assume it would override any prior action that still eludes me.

    #.bashrc
    sudo iwconfig wlan0 power off

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.