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?

2 Answers2

14

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 –  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
  • 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
  • Ubuntu 22.04, nothing works except for the Service Unit option. Depending on what task is being executed in the service, especially if persistence is an issue, you might want to configure KillMode=none/mixed/process in the [Service] section... – Makaveli84 Sep 24 '23 at 08:48
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
  • 105,154
  • 20
  • 279
  • 497