[Solved] How run a script when waking up from sleep?

Please post suggestions for improvement of Cinnamon here
https://github.com/linuxmint/Cinnamon
Post Reply
Pierre Thibault
Level 1
Level 1
Posts: 16
Joined: Wed Jun 13, 2018 11:13 am

[Solved] How run a script when waking up from sleep?

Post by Pierre Thibault » Sat Sep 15, 2018 9:27 am

I would like to run a script each time Cinnamon wakes up from sleep. An idea on how to achieve this?
Last edited by Pierre Thibault on Sat Sep 15, 2018 2:33 pm, edited 1 time in total.

User avatar
catweazel
Level 17
Level 17
Posts: 7497
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: How run a script when waking up from sleep?

Post by catweazel » Sat Sep 15, 2018 9:42 am

Pierre Thibault wrote:
Sat Sep 15, 2018 9:27 am
I would like to run a script each time Cinnamon wakes up from sleep. An idea on how to achieve this?
What version of Mint?
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

Pierre Thibault
Level 1
Level 1
Posts: 16
Joined: Wed Jun 13, 2018 11:13 am

Re: How run a script when waking up from sleep?

Post by Pierre Thibault » Sat Sep 15, 2018 9:52 am

19

User avatar
catweazel
Level 17
Level 17
Posts: 7497
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: How run a script when waking up from sleep?

Post by catweazel » Sat Sep 15, 2018 10:01 am

Pierre Thibault wrote:
Sat Sep 15, 2018 9:52 am
19
Probably the best way to do it then is with a systemd service.

viewtopic.php?f=42&t=275464

Your service definition will need to look something like this but you might have to research it a bit to verify it:

Code: Select all

[Unit] 
Description=Start my custom script
Before=sleep.target
StopWhenUnneeded=yes

[Service] 
Type=oneshot 
RemainAfterExit=yes 
ExecStop=-/path/to/script.sh

[Install] 
WantedBy=sleep.target
¡uʍop ǝpısdn sı buıɥʇʎɹǝʌǝ os ɐıןɐɹʇsnɐ ɯoɹɟ ɯ,ı

gm10
Level 10
Level 10
Posts: 3403
Joined: Thu Jun 21, 2018 5:11 pm

Re: How run a script when waking up from sleep?

Post by gm10 » Sat Sep 15, 2018 10:05 am

^ that (although I believe his example may run "Before" sleep instead of "After") or in a simpler approach you can just create a script like this, adding your own commands where it says:

Code: Select all

sudo tee /lib/systemd/system-sleep/wakeup_script <<'EOB'
#!/bin/sh
case $1 in
    post)
        #COMMANDS GO HERE
        ;;
esac
EOB
and make it executable:

Code: Select all

sudo chmod +x /lib/systemd/system-sleep/wakeup_script
Don't use pm-utils to invoke sleep, neither of our solutions will work with that (if you don't know what that is, just don't worry about it).

Pierre Thibault
Level 1
Level 1
Posts: 16
Joined: Wed Jun 13, 2018 11:13 am

Re: How run a script when waking up from sleep?

Post by Pierre Thibault » Sat Sep 15, 2018 11:05 am

Thank you. But I think my machine does not sleep. So I am not able to test for the moment.

gm10
Level 10
Level 10
Posts: 3403
Joined: Thu Jun 21, 2018 5:11 pm

Re: How run a script when waking up from sleep?

Post by gm10 » Sat Sep 15, 2018 11:11 am

Pierre Thibault wrote:
Sat Sep 15, 2018 11:05 am
But I think my machine does not sleep. So I am not able to test for the moment.
?

You can suspend you machine from the shutdown menu.

Pierre Thibault
Level 1
Level 1
Posts: 16
Joined: Wed Jun 13, 2018 11:13 am

Re: How run a script when waking up from sleep?

Post by Pierre Thibault » Sat Sep 15, 2018 11:15 am

This functionality does not seem to be well supported on the hardware I am using. After clicking sleep do I have to wait a certain delay to have my machine to really sleep? Because it seems my machine is not sleeping.

gm10
Level 10
Level 10
Posts: 3403
Joined: Thu Jun 21, 2018 5:11 pm

Re: How run a script when waking up from sleep?

Post by gm10 » Sat Sep 15, 2018 11:22 am

Pierre Thibault wrote:
Sat Sep 15, 2018 11:15 am
This functionality does not seem to be well supported on the hardware I am using. After clicking sleep do I have to wait a certain delay to have my machine to really sleep? Because it seems my machine is not sleeping.
No, should be instant. You can try:

Code: Select all

systemctl suspend
in a terminal. Otherwise a reboot may fix this. I'm assuming based on the topic of this thread that suspend has worked before.

Pierre Thibault
Level 1
Level 1
Posts: 16
Joined: Wed Jun 13, 2018 11:13 am

Re: How run a script when waking up from sleep?

Post by Pierre Thibault » Sat Sep 15, 2018 11:39 am

I installed Linux Mint recently and I am discovering by posting here that sleep is not working well.
gm10 wrote:
Sat Sep 15, 2018 11:22 am

No, should be instant. You can try:

Code: Select all

systemctl suspend
When I do this command it seems to go to sleep but it then wakes up after a second and I then I see the locking screen.

I am running LinuxMint on a Macbook Pro retina Mid '15. I realize now that to make the computer to sleep I had to close the lid at the same time I choose the sleep command otherwise it wakes up just like I described.

I wanted to run a script at wake up time because I thought that Cinnamon was resetting my custom keyboard each time my machine is waking up. It may not be the case after all.

Sorry for the confusion. As you can see I am confused myself. The sleep behaviour seems erratic on my machine.

gm10
Level 10
Level 10
Posts: 3403
Joined: Thu Jun 21, 2018 5:11 pm

Re: How run a script when waking up from sleep?

Post by gm10 » Sat Sep 15, 2018 1:30 pm

Might be broken lid detection, i.e. it wakes you up again right away. You can usually disable the lid wakeup setting in the UEFI/BIOS.

You could also disable lid detection on the operating system level, although I'm not as confident that it would help in this specific case:

Code: Select all

sudo sed -i '/IgnoreLid=/{s/false/true/}' /etc/UPower/UPower.conf;sudo sed -i '/HandleLidSwitch/{s/#//;s/suspend/ignore/}' /etc/systemd/logind.conf
Anyway, we gave you two viable ways to run scripts at sleep events, so see if they help you out with the keyboard. Otherwise it's probably best to make a new thread about the keyboard stuff if you need help with that, I think the rest of this thread would otherwise just be confusing to that discussion.

Pierre Thibault
Level 1
Level 1
Posts: 16
Joined: Wed Jun 13, 2018 11:13 am

Re: [Solved] How run a script when waking up from sleep?

Post by Pierre Thibault » Sat Sep 15, 2018 2:34 pm

Yes, thank you gm10. I was starting to diverge.

Post Reply

Return to “Cinnamon”