9

When my laptop resumes from sleep, the wifi connection doesn't resume and I have to manually restart network manager every time with sudo service network-manager restart from terminal.

I had a script that did that, but it prevented the PC from properly resuming from sleep so I had to remove it.

2 Answers 2

13

You should be able to create a script in the /etc/pm/sleep.d/ directory (or supposedly the /lib/systemd/system-sleep/ directory if you are running 15.10+) which executes that restart command on system resume. Make sure to make that script executable.

  1. Create a network_restart file in said directory with these contents:

    case "${1}" in
      resume|thaw)
        sudo service network-manager restart
    ;;
    esac
    
  2. Make the file executable: sudo chmod +x network_restart
1
  • 1
    That's a great solution. I'm using it to restart nm-applet which is losing it after suspend.
    – Greg
    May 9, 2018 at 8:25
7

For systemd on Ubuntu 16.04 you should also consider the "pre" (suspend) and "post" (resume) argument in ${1} so that the restart executes only after leaving system suspend.

I also had to add a sleep to give the network scan enough time to complete (my best guess).

$ sudo cat /lib/systemd/system-sleep/network-manager-restart 
#!/bin/sh
set -e

if [ "$2" = "suspend" ] || [ "$2" = "hybrid-sleep" ]; then
    case "$1" in
        post) sleep 10 ; systemctl restart network-manager ;;
    esac
fi

For more details read: https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html

Immediately before entering system suspend and/or hibernation systemd-suspend.service (and the other mentioned units, respectively) will run all executables in /usr/lib/systemd/system-sleep/ and pass two arguments to them. The first argument will be "pre", the second either "suspend", "hibernate", or "hybrid-sleep" depending on the chosen action. Immediately after leaving system suspend and/or hibernation the same executables are run, but the first argument is now "post". All executables in this directory are executed in parallel, and execution of the action is not continued until all executables have finished.

1
  • 1
    "pre" and "post" are actually passed in as the first argument, $1, contrary to what the first paragraph says. (The code sample is correct.) I'd suggest an edit, but it's only one character :)
    – Ben
    Feb 21, 2017 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .