You are not logged in.

#1 2021-02-08 17:08:31

arch181
Member
Registered: 2021-01-26
Posts: 35

[SOLVED]Running a script on suspend

Hi guys, I'm just wondering if there is any way that I could run a script on system suspend.
I already tried to make a file in /lib/systemd/system-sleep/ with this content (The lockscreen script is executable):
   

#!/bin/sh

if [ "${1}" == "pre" ]; then
        sh /home/yan/.config/scripts/lockscreen
elif [ "${1}" == "post" ]; then
#Nothing 
fi

Anyone has any ideas, how can I do that??
Thanks for any kind of help.

Last edited by arch181 (2021-02-09 09:17:46)

Offline

#2 2021-02-08 17:22:16

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED]Running a script on suspend


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2021-02-08 20:56:34

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

Slithery
Can you please detail a bit because i think i tried everything that is written there maybe im doing something wrong idk...
I tried to make a suspend@.service file in /etc/systemd/system/ like this:

[Unit]
Description=User suspend actions
Before=suspend.target 

[Service]
User=%I
ExecStart=/home/yan/.config/scripts/lockscreen

[Install]
WantedBy=suspend.target

i Started the service by typing sudo systemctl enable suspend@user rebooted the laptop and nothing worked.

I also tried to change my autostart.sh file in /usr/lib/systemd/system-sleep/ to see if it changes anything like this:

#!/bin/sh
case $1 in
        pre/*)
        /home/yan/.config/scripts/lockscreen
        ;;
        post/*)
        #Nothing
        ;;
esa

And still nothing seems to work.

Last edited by arch181 (2021-02-08 21:13:13)

Offline

#4 2021-02-08 21:10:35

loqs
Member
Registered: 2014-03-06
Posts: 19,155

Re: [SOLVED]Running a script on suspend

arch181 wrote:

i Started the service by typing sudo systemctl enable suspend@user rebooted the laptop and nothing worked.

Your user has the login name user?

Offline

#5 2021-02-08 21:16:58

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

loqs wrote:
arch181 wrote:

i Started the service by typing sudo systemctl enable suspend@user rebooted the laptop and nothing worked.

loqs
Your user has the login name user?

No, maybe that was the problrm that i need to change to my username??
Let me check and i'll be back

Offline

#6 2021-02-08 21:26:22

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

loqs
Still not working

Offline

#7 2021-02-08 21:36:13

loqs
Member
Registered: 2014-03-06
Posts: 19,155

Re: [SOLVED]Running a script on suspend

Does the system journal the service started after you suspend the system?

Offline

#8 2021-02-08 21:39:18

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

loqs
Can you tell me please how can i do that or point me to any arch wiki that will give me the answer on how to check the journal?

Offline

#9 2021-02-08 21:40:33

loqs
Member
Registered: 2014-03-06
Posts: 19,155

Re: [SOLVED]Running a script on suspend

Offline

#10 2021-02-08 21:44:30

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

loqs
I just found out that the new suspend service failed and this is the message systemctl status suspend@yan.service:

 Loaded: loaded (/etc/systemd/system/suspend@.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Mon 2021-02-08 23:24:57 IST; 19min ago
    Process: 1046 ExecStart=/home/yan/.config/i3/scripts/lockscreen (code=exited, status=1/FAILURE)
   Main PID: 1046 (code=exited, status=1/FAILURE)

Feb 08 23:24:57 arch181 systemd[1]: Started User suspend actions.
Feb 08 23:24:57 arch181 lockscreen[1048]: i3lock: Could not connect to X11, maybe you need to set DISPLAY?
Feb 08 23:24:57 arch181 systemd[1]: suspend@yan.service: Main process exited, code=exited, status=1/FAILURE
Feb 08 23:24:57 arch181 systemd[1]: suspend@yan.service: Failed with result 'exit-code'.

Last edited by arch181 (2021-02-08 21:47:57)

Offline

#11 2021-02-08 22:16:39

loqs
Member
Registered: 2014-03-06
Posts: 19,155

Re: [SOLVED]Running a script on suspend

The first example from the wiki page used:

Environment=DISPLAY=:0

Offline

#12 2021-02-08 22:17:05

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

i added Environment=DISPLAY=:0 to my suspend@.service file like this:

[Unit]
Description=User suspend actions
Before=suspend.target

[Service]
User=yan
Environment=DISPLAY=:0
ExecStart=/home/yan/.config/i3/scripts/lockscreen

[Install]
WantedBy=suspend.target

And it stopped failing but still the lockscreen script isnt executed.

Offline

#13 2021-02-08 22:19:04

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

loqs wrote:

The first example from the wiki page used:

Environment=DISPLAY=:0

loqs
already did that, i think im in the right path to get it working thanks smile

Last edited by arch181 (2021-02-08 22:24:08)

Offline

#14 2021-02-09 07:45:57

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 79,083

Re: [SOLVED]Running a script on suspend

Updated status?
What makes you believe the script isn't executed when previously it ran but failed on the i3lock invocation because of the missing DISPLAY environment?
Do you still have/try the hook? Did you export the DISPLAY there as well?

Please also post the actual script.

Online

#15 2021-02-09 08:22:41

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

seth wrote:

Updated status?
What makes you believe the script isn't executed when previously it ran but failed on the i3lock invocation because of the missing DISPLAY environment?
Do you still have/try the hook? Did you export the DISPLAY there as well?

Please also post the actual script.

seth
The script is the regular i3lock screen  wrapped with another script that i got from github to costumaize the i3lock and it runs whenever i press the keybindings that have in the i3 config file.
Here is the script from github:

#!/bin/sh

B='#42424299'  # blank
C='#42424299'  # clear ish
D='#42424299'  # default
T='#00838fB3'  # text
W='#880000bb'  # wrong
V='#424242'  # verifying

/usr/bin/i3lock \
--insidevercolor=$C   \
--ringvercolor=$V     \
\
--insidewrongcolor=$C \
--ringwrongcolor=$W   \
\
--insidecolor=$B      \
--ringcolor=$D        \
--linecolor=$B        \
--separatorcolor=$D   \
\
--verifcolor=$T        \
--wrongcolor=$T        \
--timecolor=$T        \
--datecolor=$T        \
--layoutcolor=$T      \
--keyhlcolor=$W       \
--bshlcolor=$W        \
\
--screen 1            \
--blur 5              \
--clock               \
--indicator           \
--timestr="%H:%M:%S"  \
--datestr="%A, %m %Y" \
#--keylayout 1         \

# --veriftext="Drinking verification can..."
# --wrongtext="Nope!"
# --textsize=20
# --modsize=10
# --timefont=comic-sans
# --datefont=monofur
# etc

Here is my suspend@.service file:

[Unit]
Description=User suspend actions
Before=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

[Service]
User=yan
Environment=DISPLAY=:0
ExecStart=/home/yan/.config/i3/scripts/lockscreen

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

Note: right now I have no fails in the systemctl services and whenever I suspend the laptop then wake it up the script gets executed and the lockscreen is displayed for like 1 second but after that it disappears and im automatically loged in.

Last edited by arch181 (2021-02-09 08:26:26)

Offline

#16 2021-02-09 08:29:00

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 79,083

Re: [SOLVED]Running a script on suspend

That doesn't fork and you're also using the wrong "Before", see eg. https://www.jvt.me/posts/2019/12/03/loc … d-systemd/

Online

#17 2021-02-09 08:47:52

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

seth wrote:

That doesn't fork and you're also using the wrong "Before", see eg. https://www.jvt.me/posts/2019/12/03/loc … d-systemd/

seth
Thanks, really appreciate it. Everything is working perfect.
So here is my lock_suspend@.service file in /etc/systemd/system/ :

[Unit]
Description=i3lock screen after suspend
Before=suspend.target sleep.target

[Service]
User=yan
Type=forking
Environment=DISPLAY=:0
ExecStart=/home/yan/.config/i3/scripts/lockscreen

[Install]
WantedBy=suspend.target sleep.target

And i just enabled it by typing sudo systemctl enable  lock_suspend@yan, then i rebooted and suspended the laptop and it worked. Thank you wink

Offline

#18 2021-02-09 08:57:31

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 79,083

Re: [SOLVED]Running a script on suspend

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

Online

#19 2021-02-09 09:11:02

arch181
Member
Registered: 2021-01-26
Posts: 35

Re: [SOLVED]Running a script on suspend

seth wrote:

Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.

seth
Done

Last edited by arch181 (2021-02-09 09:19:23)

Offline

Board footer

Powered by FluxBB