7

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 09:42
  • no it should be at /lib/systemed/systemed-sleep/ but don't know how to make it work after that. –  Aug 15 '15 at 09:44
  • Try [this question](http://askubuntu.com/q/92218/178596) - though a deleted answer on it suggests elsewhere using another location due to [this](https://launchpad.net/ubuntu/+source/pm-utils/+bug/1455097) . – Wilf Aug 15 '15 at 09:45

2 Answers2

13

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
Martin Thornton
  • 5,151
  • 11
  • 30
  • 39
  • 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 –  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. –  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 –  Aug 15 '15 at 16:59
  • Only from the most recent run, starting from `Aug 15 ??:??:?? ????? systemd[1]: Started Run start_my_connection.`. [Edit] it into your answer. – Martin Thornton Aug 15 '15 at 17:09
  • this is a Google docs link to it https://docs.google.com/document/d/1_QLTgHmx8yi9ACEmJZqgWFJwk9-MlsHMCoUd9D2Ue6o/edit?usp=sharing –  Aug 15 '15 at 17:12
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/27004/discussion-between-martin-thornton-and-ronniedroid). – Martin Thornton Aug 15 '15 at 17:16
  • Thank you for the great tips! it is better to have it `Before` than `After` `suspend.target`, otherwise the contents of the screen are visible for a fraction of a second after hibernation, which is a "privacy concern" – Rolf Mar 29 '18 at 04:24
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

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492