7

When my debian jessie desktop box wakes up from sleep (via the new shiny systemd) my mouse settings are returned to their defaults, having reset my customisation

xinput set-prop 12 'Device Accel Constant Deceleration' 2.5

which runs when I log in.

how can I run an arbitrary user script on wakeup? (assume that the user is the owner of the X session)

As far as I can recall, the following is the only customisation I've made of the systemd setup (yes, I know it's completely wrong because it doesn't work for arbitrary users, but I've not worked out how to do that yet... this is somewhat related)

additionally, how can I run an arbitrary user script before wakeup, as the user who is currently using the X screen?

cat /etc/systemd/system/i3lock.service

#systemctl enable i3lock.service

[Unit]
Description=i3lock
Before=sleep.target

[Service]
User=fommil
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/i3lock -c 000000

[Install]
WantedBy=sleep.target
2

I created a file in /lib/systemd/system-sleep.

#!/bin/sh
case $1 in
  post)
    /bin/runme
  ;;
esac

The post is called on wakeup.

EDIT:

https://www.freedesktop.org/software/systemd/man/systemd-suspend.service.html

  • 1
    This didn't work for me. Can you link to some documentation explaining how it's supposed to work? – Ryan Lue Jul 22 '18 at 9:34
  • This doesn't work for me either. The context this script runs in is extremely limited, and does not run as the user who was active when the system went to sleep. So it does not get access to the display (which excludes most desktop utilities here), info/variables for the user who is logged in, or anything else related to the user's session who is currently logged in. I need a script to run as the user who is logged in inside the context of the desktop session. I still haven't found anything to do this but I will update here if I do. – theferrit32 Dec 20 '18 at 23:10
  • @theferrit32 you should be able change to your preferred user with the 'su' command from within your script. – Kirjain Jun 10 at 5:43
2

This answer is based on askubuntu.com/a/661747/394818 (as also referred to in the comment by @sun-bear), askubuntu.com/q/616272/394818 and superuser.com/a/1269158/585953.

Using a system service:

Create the file /etc/systemd/system/my_user_script.service:

[Unit]
Description=Run my_user_script
After=suspend.target hibernate.target hybrid-sleep.target

[Service]
ExecStart=/path/to/my_user_script
#User=my_user_name
#Environment=DISPLAY=:0

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

Remove suspend/hibernate/hybrid in case the service should only be executed after waking up from a specific type of sleep. In case the service needs to be ran by a specific user, uncomment the User= and Environment= lines and replace the relevant user name.

Install the service file with:

sudo systemctl enable my_user_script

Using a user service will not work:

In order to avoid setting a hard coded user name with User=, one could create the exact same service file at .local/share/systemd/user/my_user_script.service and activate with systemctl --user enable my_user_script. However, that will not work. @grawity explains in more detail at unix.stackexchange.com/a/174837/163108 why that is:

sleep.target is specific to system services. The reason is, sleep.target is not a magic target that automatically gets activated when going to sleep. It's just a regular target that puts the system to sleep – so the 'user' instances of course won't have an equivalent. (And unfortunately the 'user' instances currently have no way to depend on systemwide services.)

1

Open this file:

sudo vim /lib/systemd/system-sleep/hdparm

Contents:

#!/bin/sh

case $1 in   post)
    /usr/lib/pm-utils/power.d/95hdparm-apm resume
    ## Paste your command to run your script
    ;; esac

Your command will execute with admin privileges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.