I created a systemctl service, to run before sleep but it runs immediately when i enable the service

as the title says.
i run: systemctl --user edit --full --force TestSuspend


# /home/mohamed/.config/systemd/user/TestSuspend.service
[Unit]
Description=Before sleep
Before=sleep.target

[Service]
Type=oneshot
ExecStart=/home/mohamed/Desktop/bash.sh

[Install]
WantedBy=sleep.target
# /home/mohamed/Desktop/bash.sh
#!/bin/bash

echo "test" >> /home/mohamed/Desktop/test.txt

then run: systemctl --user enable --now TestSuspend

I get this message:
Unit /home/mohamed/.config/systemd/user/TestSuspend.service is added as a dependency to a non-existent unit sleep.target.

the script is run immediately and not before sleep

You’re running the service straight away using --now…so remove it from the command.

1 Like

Just a guess:
there is no sleep.target in the --user context
that is a system wide target :man_shrugging:

2 Likes

removed the --now, the script didn’t run immediately but didn’t run before sleep either (no file created on the Desktop)

Sorry for not being clearer…you had 2 problems, now you have 1. :smiley:

Need to refresh my memory on the sleep part, but it’s late here, so we’ll see…have you looked into @Nachlese’s suggestion?

1 Like

it works now.
sudo systemctl edit --full --force TestSuspend2

sudo systemctl enable TestSuspend2

1 Like

systemctl command does not need sudo - you will be asked for permission when needed

3 Likes

It does for the first command.

for me: no sudo needed - but no timeout or period where the authorization is kept, I get asked every time
no matter whether TTY or GUI :man_shrugging:

Not when it needs to create the file:

$ systemctl edit --full --force blabla.service
Failed to create temporary file for '/etc/systemd/system/blabla.service': Permission denied
1 Like

Ah - I misunderstood you when you said “first command”.
You meant the first one in the post above, I meant the time in between asked for a pw like when using sudo.

I have never edited a systemd service and thus never encountered this - start, stop, enable, disable, restart all ask for it every time.

1 Like

I have a problem where if I use the video-linux driver, my system won’t wake up from sleep if docker-desktop is running.
so I’m trying to create a service that stops docker-desktop before sleep.
here are my failed attempts:

# /etc/systemd/system/TestSuspend2.service
$[Unit]
Description=Before sleep
Before=sleep.target suspend.target

[Service]
Type=oneshot
#ExecStart=/usr/bin/systemctl --user stop docker-desktop
#ExecStart=/home/mohamed/Desktop/bash.sh
ExecStart=/usr/bin/sudo -u mohamed /home/mohamed/Desktop/bash.sh

[Install]
WantedBy=sleep.target suspend.target
# /home/mohamed/Desktop/bash.sh
#!/bin/bash
export XDG_RUNTIME_DIR=/run/user/1000
systemctl --user stop docker-desktop

Your previous post is likely the best place for this question:

1 Like

The OP’s same general topic continues here:

…for anyone interested.

I fixed the problem of that post, I was just testing the service with echo. but with this post, I’m asking about why “systemctl --user stop docker-desktop” doesn’t work in the service.

My recommendation remains;

However, you might wish to remove the $ from that script:

Cheers.

2 Likes

thank you, I didn’t realize it. it works now.

1 Like

Please do not create duplicate topics. I’ve merged them.

Seems like something like this should be in one’s handy toolbox, whether or not this page works, is a different question:
https://www.shellcheck.net/

edit: It’s a Haskell program so you can install it also. Works quite well, I might add. 8)

2 Likes