Run script on wake assistance

Always good to explain what I am hoping to accomplish, as I maybe asking the wrong question.

I am running SDDM instead of GDM.

Because of that I use i3lock to lock my PC as GDM is required to lock modern GNOME systems.

I have sorted my keyboard shortcut META+L and it works perfectly, however I sometimes put my PC to sleep and I want it to lock automatically on resume.

Following this: I have a shell script in

export DISPLAY=:0

case $1/$2 in
    echo "Going to $2..."
    # Place your pre suspend commands here, or `exit 0` if no pre suspend action required
    exit 0
    echo "Waking up from $2..."
    # Place your post suspend (resume) commands here, or `exit 0` if no post suspend action required
    i3lock -c 000000 -n

The display 0 line is because nothing happens and checking the log of

journalctl -b -u systemd-suspend.service

I can see:

Jun 08 18:13:10 fedora systemd-sleep[12070]: System returned from sleep state.
Jun 08 18:13:10 fedora systemd-sleep[12232]: No protocol specified
Jun 08 18:13:10 fedora systemd-sleep[12232]: i3lock: Could not connect to X11, maybe you need to set DISPLAY?
Jun 08 18:13:10 fedora [12226]: /usr/lib/systemd/system-sleep/ failed with exit status 1.
Jun 08 18:13:10 fedora systemd[1]: systemd-suspend.service: Deactivated successfully.
Jun 08 18:13:10 fedora systemd[1]: Finished systemd-suspend.service - System Suspend.

I have tried adding DISPLAY to =:0 which didn’t help, and also tried enabling a service which also didn’t help.

The service in question was

Description=Run On Wakeup Script

ExecStart=Shell script above


Once enabled with

sudo systemctl enable onwakeup

It doesn’t even show up on logs.

Any advice?

I have solved using the following tutorial but adapting Before to After as I want it to run After resuming.

Create a file using sudo at /etc/systemd/system/wakelock@.service, with the following content:

Description=Lock the screen on resume from suspend

ExecStart=i3lock -c 000000 -n

Step 3: Enable the systemd module
Enable the systemd module you just created with:

sudo systemctl enable wakelock@<YOUR-USER-NAME> --now
i3lock will now run once as a demo, then when you wake from suspend, i3lock will automatically start.

That's it! If you make any changes and want them reflected immediately without restart, simply do:

sudo systemctl restart wakelock@<YOUR-USER-NAME>
1 Like