6

I have a script that starts my broadband connection at start-up and I want to make it start at resume from suspend.

My script's is /usr/local/bin/start_my_connection

it contains:

#!/bin/sh
sleep 10
nmcli nm wwan on
nmcli con up id "reber connection"`

What should I do to make it run using systemd?

  • Don't know if it's relevant to systemd. Why not try putting it into /etc/pm/sleep.d/, and check what happens. – mikewhatever Aug 15 '15 at 9:42
  • no it should be at /lib/systemed/systemed-sleep/ but don't know how to make it work after that. – RonnieDroid Aug 15 '15 at 9:44
  • Try this question - though a deleted answer on it suggests elsewhere using another location due to this . – Wilf Aug 15 '15 at 9:45
12

There are two approaches to choose from:

Using the /lib/systemd/system-sleep/ directory:

Create another script called 00start_my_connection:

#!/bin/sh
if [ $1 = post ] && [ $2 = suspend ]
then /usr/local/bin/start_my_connection
fi

$1 is "post" on resume/thaw and "pre" otherwise. In either case, $2 contains either "suspend", "hibernate", or "hybrid-sleep". If you want the script to also run on thaw from hibernation, leave out && [ $2 = suspend ].

Ensure this script is executable by using chmod a+x 00start_my_connection

Move this script into /lib/systemd/system-sleep/ using

sudo mv 00start_my_connection /lib/systemd/system-sleep/

Using service files:

Create the file /etc/systemd/system/start_my_connection.service:

[Unit]
Description=Run start_my_connection
After=suspend.target
#After=hibernate.target
#After=hybrid-sleep.target

[Service]
ExecStart=/usr/local/bin/start_my_connection

[Install]
WantedBy=suspend.target
#WantedBy=hibernate.target
#WantedBy=hybrid-sleep.target

Uncomment all lines if you also want the script to run on thaw from hibernation. Then install the service file with:

sudo systemctl enable start_my_connection.service
  • hi, the first method didn't work for me, the second one when i try to install the serves it says no such file or directory – RonnieDroid Aug 15 '15 at 16:24
  • Does the file /etc/systemd/system/start_my_connection.service exist? – Martin Thornton Aug 15 '15 at 16:29
  • sorry my bad, i place it at /bin/systemed not /etc/systemed . now it's installed but still doesn't reconnect after suspend. – RonnieDroid Aug 15 '15 at 16:46
  • What is the output of journalctl -eu start_my_connection – Martin Thornton Aug 15 '15 at 16:53
  • it's a long list, post it here? @martin – RonnieDroid Aug 15 '15 at 16:59
3

Create a file 01myscript in /etc/pm/sleep.d/ directory.

Contents of that file should be:

#!/bin/bash

case $1 in 
    thaw|resume) /usr/local/bin/start_my_connection
    ;;
esac

Make that script executable: sudo chmod +x /etc/pm/sleep.d/01myscript.

Try to suspend

  • Is reboot or restart sleep service needed? – WinEunuuchs2Unix Oct 28 '18 at 16:05
  • @WinEunuuchs2Unix I've not used this method for a long time but if I recall correctly no reboot or service restart was required. – Sergiy Kolodyazhnyy Oct 28 '18 at 22:06
  • I was working with this after comment and can confirm you don't need to restart systemd-suspend.service – WinEunuuchs2Unix Oct 28 '18 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.