3

I have a custom script (uses $DISPLAY) for setting up the extra buttons on my mouse. I want to run this script every time I log into the system. I have added the script as a Startup Application via the Ubuntu (18.10) GUI (Gnome 3.30.1). It runs after I turn on my PC or reboot it, but fails to run after the PC resumes from sleep.

How can I make the script run after the PC resumes from sleep? (Preferably without having to maintain a separate script).

Naximus
  • 131
  • 1
    Read man pm-action to see how to add a script to Suspend/Resume/Sleep/Wake. You'll have to hard-code the value of $DISPLAY, and consider xhost +localhost to give the script access to your X windows – waltinator Nov 15 '18 at 17:34
  • Is pm-suspend different from the suspend option in Gnome3 or is it the same thing? After resuming from a pm-suspend I don't get a login screen but after resuming from a normal sleep I do see the login screen. – Naximus Nov 15 '18 at 19:15
  • Ok it looks like my system uses systemctl suspend to goto sleep. How can I configure it to run a custom script after I login? – Naximus Nov 15 '18 at 19:19
  • 1
    Placing a script in /lib/systemd/system-sleep/ does work but now its unable to access the DISPLAY variable. Exporting it as :0 don't help either. – Naximus Nov 15 '18 at 20:26
  • 1
    duplicate of https://askubuntu.com/questions/226278/run-script-on-wakeup & https://askubuntu.com/questions/92218/how-to-execute-a-command-after-resume-from-suspend. –  Nov 16 '18 at 01:41

2 Answers2

6

Run Script when Resuming from Suspend

Create a new file /lib/systemd/system-sleep/resume and copy in:

#!/bin/sh

case $1/$2 in pre/) echo "Going to $2..." # Place your pre suspend commands here, or exit 0 # if no pre suspend action required exit 0 ;; post/) echo "Waking up from $2..." # Place your post suspend (resume) commands here, or exit 0 # if no post suspend action required mouse_script.sh ;; esac

NOTE: replace user mouse_script.sh (third line from the bottom) with your script name. Provide the full path name if the script is not in your path (echo $PATH).

Then mark it executable with the command:

sudo chmod +x /lib/systemd/system-sleep/resume
-1

You can call it from /etc/init.d/rc.local

sudo nano /etc/init.d/rc.local

and add the path to your script