Score:2

Network related part of a script will not run in /lib/systemd/system-sleep

us flag

The purpose of the script is to re-enable a hidden SSID upon resume from sleep. Somehow the ability to restart a hidden SSID automatically was lost when I upgraded from 18.04 LTS to 20.04 LTS.

You can see from the script below how many ways I have tried to get this to work. The echo print to suspend.txt works every time upon resume. It is just the portion to restart the wifi that does not work.
All the commands I tried in the script work as expected when entered on the CLI and the /home/user/Desktop/wifi-ssid.sh works when double clicked.

The results from journalctl | grep systemd-sleep is below the script. Most notable is the error...

"Error: Connection activation failed: No suitable device found for this connection (device lo not available because device is strictly unmanaged)."

This must be something simple. Please help.


#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/home/user/Desktop

case "$1" in
    pre)
            #code execution BEFORE sleeping/hibernating/suspending
    ;;
    post)
            #code execution AFTER resuming
            echo "$(date) - $1: test pre" >> /home/user/tmp/suspend.txt
            /usr/bin/nmcli c up id "SSID"
            #/usr/bin/nmcli radio wifi on
          #sleep 2
          #/usr/bin/nmcli c up id "SSID"
            #/home/user/Desktop/wifi-ssid.sh
            #su user -c "/home/user/Desktop/wifi-ssid.sh"
            #su user -c "/usr/bin/nmcli c up id "SSID""
            #/usr/bin/sudo -u user bash -c "export XDG_RUNTIME_DIR=/run/user/1000; /home/user/Desktop/wifi-ssid.sh"
            #/usr/bin/sudo -u user bash -c "export XDG_RUNTIME_DIR=/run/user/1000; /usr/bin/nmcli c up id "SSID""
            #/usr/bin/sudo -u user bash -c "/home/user/Desktop/wifi-ssid.sh"            
            #su user -c "/usr/bin/nmcli con up SSID"
            #/usr/bin/sudo -u user bash -c "export XDG_RUNTIME_DIR=/run/user/1000; /usr/bin/nmcli con up SSID; /usr/bin/nmcli radio wifi on; sleep 2; /usr/bin/nmcli c up id "SSID""
            
     ;;
esac

exit 0

journalctl | grep systemd-sleep shows the following:

Mar 03 16:53:30 User systemd-sleep[4074]: Suspending system...  
Mar 03 20:01:32 User systemd-sleep[4074]: System resumed.  
Mar 03 20:01:32 User systemd-sleep[4206]: /dev/sda:  
Mar 03 20:01:32 User systemd-sleep[4206]:  setting Advanced Power Management level to 0xfe (254)  
Mar 03 20:01:32 USer systemd-sleep[4206]:  APM_level = 0  
Mar 03 20:01:32 User systemd-sleep[4189]: Error: Connection activation failed: No suitable device found for this connection (device lo not available because device is strictly unmanaged).
Score:3
jp flag

Networking is not made available until all scripts in the /lib/systemd/system-sleep directory finish executing.

Try sending networking related commands to the background with a delay of e.g. 10 seconds in a sub-shell and detach it i.e. change:

/usr/bin/nmcli c up id "SSID"

to:

(sleep 10; /usr/bin/nmcli c up id "SSID") & disown

Notice that disown is a bash builtin and depending on your system shell configuration /bin/sh might be linked to a different shell command interpreter other than bash .... For such case, you might call /bin/bash using a command string like so:

/bin/bash -c '(sleep 10; /usr/bin/nmcli c up id "SSID") & disown'

Or even in most cases calling /bin/sh with a sub-shell in a command string i.e. like so:

/bin/sh -c '(sleep 10; /usr/bin/nmcli c up id "SSID") &'

should fork a new detached process.

Another thing that you might need to keep that process alive is to explicitly specify/switch to a user e.g. your username or even root if your command/script requires elevated privileges to execute ... You can do this as well in a command string style but, you need to use different quoting style i.e. double quotes /bin/su username -c "/bin/bash -c '...'" e.g. like so:

/bin/su username -c "/bin/bash -c '(sleep 10; /usr/bin/nmcli c up id SSID) & disown'"

changing username to a valid account's user name on the system e.g. your actual account login user name.

toddk63 avatar
us flag
Your last two suggestions did not work, but the 'Error:' went away in journalctl.The first suggestion returned an error ':disown: not found'
Raffa avatar
jp flag
@toddk63 I have updated the answer for that one on how to keep the background process alive until it does its job.
toddk63 avatar
us flag
It worked!...with "/bin/su root" but not "/bin/su username". Thank you for the help.
Raffa avatar
jp flag
@toddk63 You are welcome ... You did change `username` to e.g. your actual account login user name ... If so, then your command requires elevated privileges to execute and specifying `root` as a user is mandatory in such case.
toddk63 avatar
us flag
Yes I did try actual "username", so "root" was required
toddk63 avatar
us flag
/bin/su root -c "/bin/bash -c '(sleep 10; /usr/bin/nmcli c up id SSID) & disown'"
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.