When putting these into the respective folders in the new laptop, it fails to goto sleep. Screen goes black and system freezes. Fan and power led is still on.
The only thing to do is long press the power button.
permission of the files is set to 755, owned by root.
The idea sounds like a joke to me, because the whole point of presentation mode is to prevent screensaver or sleep.Why don’t you just turn it off, if you do want the pc to fall asleep. And adjust the power settings.
Because when you’re done with the presentation, you press sleep, put the laptop in your bag, go to an office, open you laptop, log in, someone comes to talk with you, you join them to their office to look at what they wanted to show you, and when you come back you realize your session is unlocked and screen is still on because you forgot to turn off presentation mode after your presentation.
You need a service file to execute your script right before your system goes to suspend/hibernate. Otherwise your script just does nothing; just existing.
That is when run as root from terminal.
Run as logged in user from terminal updates the setting.
Run with su as root from terminal also updates the setting.
As the systemd scripts are run as root, the su seem like it has to be there.
Any idea why the command is causing the system to freeze when going to sleep, while running good from terminal?
Also it was running good as a pre-sleep script in my previous laptop.
Could it be that it has to run before something else that is triggered when going to sleep? How can I make the script trigger before everything else?
Generally, you can set your service to run after or before every other service. In the options Target or Wanted by. But where exactly it should go, i do not know. You can play with --list-dependencies to see where you can glue it.
What ever I try, the script seems to run as the last thing. I’d like it to run as the first thing when sleep is called.
I may misunderstand the documentation… My understanding is that if a service is RequiredBy=sleep.target the sleep progress should stop and wait for that service to finish. But it doesn’t seem to work that way.
Using before or after swaps the order of logged lines by one: Using “before” the script is logged to run before sleep.target. Which sounds like the expected behavior, but after everything else.
The first thing to stop is NetworkManager, happens even before that. NetworkManager is actually logged as stopped before the first line containing systemd. How does the NetworkManager get to idea that system is going to sleep before systemd?
It does not trigger at all if I use any of these:
WantedBy=systemd-logind.service
WantedBy=multi-user.target
Forget to turn off presentation mode once, when some funny/evil guy is around, and you will appreciate the functionality of the script. (Or some children)
As shown, letting sleep.target run the script, as it is designed to do, or create a service RequiredBy=sleep.target to run the script doesn’t make any difference. The script is run after everything else is stopped. It needs to be run when sleep is called, before everything else is triggered.
NetworkManager seems to know sleep is called, before sleep.target is called. Hence it should be possible. But how does NetworkManager know this?
[tracy@daphne network-online.target.wants]$ cat NetworkManager-wait-online.service
[Unit]
Description=Network Manager Wait Online
Documentation=man:NetworkManager-wait-online.service(8)
Requires=NetworkManager.service
After=NetworkManager.service
Before=network-online.target
[Service]
# `nm-online -s` waits until the point when NetworkManager logs
# "startup complete". That is when startup actions are settled and
# devices and profiles reached a conclusive activated or deactivated
# state. It depends on which profiles are configured to autoconnect and
# also depends on profile settings like ipv4.may-fail/ipv6.may-fail,
# which affect when a profile is considered fully activated.
# Check NetworkManager logs to find out why wait-online takes a certain
# time.
Type=oneshot
ExecStart=/usr/bin/nm-online -s -q
RemainAfterExit=yes
# Set $NM_ONLINE_TIMEOUT variable for timeout in seconds.
# Edit with `systemctl edit NetworkManager-wait-online`.
#
# Note, this timeout should commonly not be reached. If your boot
# gets delayed too long, then the solution is usually not to decrease
# the timeout, but to fix your setup so that the connected state
# gets reached earlier.
Environment=NM_ONLINE_TIMEOUT=60
[Install]
WantedBy=network-online.target
I don’t understand how to make use of that. Because network manager doesn’t start or stop at sleep event, but it gets the instruction to sleep.
Log looks like this:
aug. 04 22:16:30 systemd-logind[827]: The system will suspend now!
aug. 04 22:16:30 NetworkManager[866]: <info> [1722802590.8682] manager: sleep: sleep requested (sleeping: no enabled: yes)
aug. 04 22:16:30 NetworkManager[866]: <info> [1722802590.8684] device (wlp2s0): state change: unavailable -> unmanaged (reason 'unmanaged-sleeping', sys-iface-state: 'managed')
aug. 04 22:16:30 NetworkManager[866]: <info> [1722802590.8699] device (p2p-dev-wlp2s0): state change: unavailable -> unmanaged (reason 'unmanaged-sleeping', sys-iface-state: 'managed')
aug. 04 22:16:30 NetworkManager[866]: <info> [1722802590.8705] manager: NetworkManager state is now ASLEEP
aug. 04 22:16:31 systemd[1]: Reached target Sleep.
Thinking about other ways to trigger… If the script could be triggered when session is locked that would also be good. At session unlock could be an (less preferred) alternative as well.
Now the script ran when lid is closed (closing lid action set to “lock session”), but the session didn’t lock. So I added this to the end of the script: xfce4-screensaver-command -l
Now script runs, and session lock when lid is closed. But still not when system is going to sleep.
My first thought when I read this was: “Oh, but that won’t trigger when system triggers the sleep.”
Then I realized I’m dumb. System won’t go to sleep when presentation mode is on. That’s the point of presentation mode.
Any way to bind this to the power button?
Edit: on further thought… I wasn’t all that dumb. When battery runs low system will trigger sleep even in presentation mode. In that situation the script won’t run when implemented this way.