Script to Restart Network Manager after Resume from Sleep

Solution 1:

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

Solution 2:

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.