Run a script, using service, before suspend

Hi,

i try to make a service turning off monitor(TV whit webos) when system suspend:

[Unit]
Description=off screen before suspend
Requires=network-online.target
Before=network-online.target
Before=suspend.target sleep.target

[Service]
ExecStartPre=/bin/sleep 5
ExecStart=/usr/bin/lgtv MyTv off ssl
Type=oneshot

[Install]
WantedBy=suspend.target sleep.target
WantedBy=network-online.target

This script WORK if i test him, but suspending manjaro make lan disconnected IMMEDIATLY (red X on lan icon) i use sleep 5 to see what’s happening while service running:

  • click suspend
  • lan disconnect immediatly
  • remote command error beaucause lan is disconnected
  • 5 seconds pass
  • system will suspend

My question: there is a way to tell at service “don’t stop ethernet when suspend?” Or configure lan to NOT disable when suspend system? Modify suspend rules?

Thx

Hi @Dareku,

I’m by no means an expert, but I think that should be After instead of Before:

After=network-online.target

Check the man page for more information:

man systemd-suspend.service

Edit:

Also see

https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/

Edit #2

From https://askubuntu.com/questions/1328663/how-to-run-a-script-before-suspend

Alternate answer using systemd

Create a file /lib/systemd/system-sleep/my_script.sh with the following content:

#!/bin/bash
case $1/$2 in
pre/*)
# commands to be executed before suspend, hibernate, etc. goes below
echo "This example does nothing"
;;
post/*)
# commands to be executed on wake, resume, etc. goes below
echo "Nothing to see here"
;;
esac

Edit and add the commands you need to add instead of the lines starting with echo above. Save the file and make it executable. See How to make a file (e.g. a .sh script) executable, so it can be run from a terminal for more.

Edit 3:

Also see:

https://unix.stackexchange.com/questions/337853/how-can-i-trigger-a-systemd-unit-on-suspend-before-networking-is-shut-down

Edit #4

And,

https://bbs.archlinux.org/viewtopic.php?id=146790

4 Likes

I’m almost certain that this WantedBy=network-online.target will make your script be executed as soon as your network is marked “online”, so remove that :wink:

Note: Don’t blindly copy over your config from your previous script in the other thread which had different requirements and goals…

The sequence of units used when booting, are not the same as those used to suspend nor resume from a suspended state…

2 Likes

as soon as i can i will try with your precious suggestions!!!

@TriMoon I’m almost certain that this WantedBy=network-online.target will make your script be executed as soon as your network is marked “online”, so remove that

i didn’t understand why i shouldn’t tell to the script to run when my network is online, like a check.

@TriMoon mentioned right after it has been started, so not when you’re shutting down the PC, AFAIK.

1 Like

That line is not a check, it tells systemd to execute your script when the network comes online.
But your script’s functionality is to put your TV OFF right?
So think about powering off your TV when you boot your PC, is that correct behavior you want? :wink:

Checks that must be valid before a service is run, are using different keywords, and none of them can check for presence of a service on a remote machine…

1 Like

Hi,
no way, i try all you suggest (thx) and read all tutorial.
Best explanation is:

According to a reply from systemd-devel mailing list, this is impossible using solely systemd while using Network Manager as Network Manager listens for the sleep signal on its own. A workaround is to put what you need to run into:

/etc/NetworkManager/dispatcher.d/pre-down.d/

i think this workaround not working well… imagine various network failure scenario and monitor flip on/off… not good.

But let’s move on to the positive results! Now i can turn off the tv during shutdown and during system startup, not bad at all!

PS: Is there a way to politely ask the manjaro developers to make script working before the network off while suspend? It could be useful to everyone.

Seems like you want to speak to the network manager or systemd devs…or maybe they need to speak to each other.


Anyway, if you aren’t already, you could use this to suspend. Bind it to a key combo if you want.

#!/usr/bin/bash

lgtv MyTv off ssl
systemctl suspend

A systemd service should work for resume. It’s late and I’m feeling too lazy to write one right now, maybe tomorrow (assuming you haven’t already got one).


Alternatively use a different network manager. Then this service should work, assuming the other network managers don’t do the same (at least systemd-networkd should hopefully work :crossed_fingers:).

[Unit]
Description=Turn off screen before suspend
Before=suspend.target

[Service]
ExecStart=/usr/bin/lgtv MyTv off ssl
Type=oneshot

[Install]
WantedBy=suspend.target

EDIT:

Here’s a resume service that should work, you may need to tweak the sleep time. I haven’t found a way to only use Before, After etc yet.

[Unit]
Description=Turn on TV after suspend
After=suspend.target

[Service]
ExecStartPre=/usr/bin/sleep 5
ExecStart=/usr/bin/lgtv MyTv on ssl
Type=oneshot

[Install]
WantedBy=suspend.target

If you change the dependencies (eg WantedBy, and possibly Before, After etc) then you should disable and re-enable the service for the changes to take effect.

1 Like

Hi,
only putting script in Network manager pre-down work. But it’s a bad workaround for me.

/etc/NetworkManager/dispatcher.d/pre-down.d/

I also create a commad to “cp” the script in pre-down folder when system sleep and “rm” at resume.

The script will move the file but won’t work, also try to launch modrpobe command to activate network, but sleep it’s absolutly prioritary than any others commands…

i am thinking to buy an ir usb adapter to control better TV. :grinning:

Maybe check man systemd-sleep
Which mentions /lib/systemd/system-sleep/ :wink:
(There is no alternative to this directory, like /etc/…)