1

I am trying to figure out wether it is possible to add some sort of script, triggered before system goes to suspend. Idea is to trigger some webhooks to store some info before idle state.

9
  • Could very well be done if you replace the default sleep/blank screen by a scripted option. Would that be acceptable? Feb 7, 2017 at 21:41
  • Yes, it would be good option. Feb 7, 2017 at 21:59
  • Thanks! Will get back to it tomorrow if no one else did in the meantime. Feb 7, 2017 at 22:00
  • Hook dirs for your triggers are in /etc/pm/
    – user535733
    Feb 8, 2017 at 4:13
  • Hi Karan, one additional question, what is your xubuntu version? Feb 8, 2017 at 17:38

1 Answer 1

0

Hooking up to something?

The challenge is that whatever hook might exist to either blank screen or going into suspend, the related command or script you want to run before would always run too late, since it would be called after the possible signal.

The solution

As always, that does not mean we ran out of options. If we disable the default blank screen/suspend -options, and replace it with our own background script, we can include the command you want to run before change of state in the procedure.

How it works

In the script below, you can set the idle time after which the screen should blank and when your system should go into suspend. You can also set the command to run before suspend.

The script

#!/usr/bin/env python3
import subprocess
import time

# set times for blank screen and suspend (in that order)
times = [300, 1200]
pre_command = "touch banana.txt"

# don't change anything below, *unless* changes for other distros
last_t = times[-1]
commands = ["xset dpms force off", "sudo xfce4-session-logout --suspend"]
restore = "xset dpms force on"

def gettime():
    return int(int(subprocess.check_output(
        "xprintidle"
        ).decode("utf-8").strip())/1000)

def run_command(cmd):
    # made it like this, so user can conveniently set commands
    subprocess.call(["/bin/bash", "-c", cmd])

def change(set_t, t1, t2):
    if all([t1 <= set_t, t2 > set_t]):
        if set_t == last_t:
            run_command(pre_command)
        run_command(commands[times.index(set_t)])
    elif all([t2 < set_t, t1 >= set_t]):
        run_command(restore)
        return True

t1 = gettime()

while True:
    time.sleep(2)
    t2 = gettime()
    for t in times:
        if change(t, t1, t2):
            break
    t1 = t2

How to set up

  1. The script needs xprintidle:

    sudo apt-get xprintidle
    
  2. Copy the script above into an empty file, save it as my_suspend.py

  3. Now we need to make sure the script can bring the computer into suspend: add a line to the sudoers file:

    • Run from a terminal:

      sudo visudo
      
    • Add the line:

      ALL All=NOPASSWD: /usr/bin/xfce4-session-logout*
      

    to the file.

  4. In the head section of the script, set the time after which you want to blank the screen (in seconds), the time you want the computer to switch to suspend and the command you want to run before suspend:

    # set times for blank screen and suspend (in that order)
    times = [300, 1200]
    pre_command = "touch banana.txt"
    

    In the example above, the screen blanks after 5 minutes, the system suspends after 20 minutes. Obviously, you want to replace the pre_command

    N.B. mind the quotes around the command! If your command also includes mixed quotes, please mention.

  5. Test- run the script from a terminal with the command:

    python3 /path/to/my_suspend.py
    

    If all works fine, add the script to Startup Applications (Session & Startup > Application Autostart (tab) > Add)

Note

The script was written for Xubuntu, but only in the line:

commands = ["xset dpms force off", "sudo xfce4-session-logout --suspend"]

The command:

"sudo xfce4-session-logout --suspend"

is Xubuntu- specific. The script therefore can easily be adapted to other distros.

6
  • Awesome! It worked very well. I appreciate your efforts. Can you please add this to Script Github as well so that more peoples can take advantage. Thank you very much. Feb 11, 2017 at 8:20
  • @KaranS.Sisodia Thanks! I love this kind of questions. I should fire up a Github account, mostly active on Launchpad :) Feb 11, 2017 at 8:25
  • Did you uploaded this to GitHub or on Launchpad? I have some test results to post. Feb 21, 2017 at 20:10
  • @KaranS.Sisodia no, I didn't, did you run into an issue? Feb 21, 2017 at 20:16
  • Not as such big issues, I just wanted to post some realtime observations like : Whan I watch youtube in full screen mode screens turned off. Another thing that I noted is screen does not turend of completely (I see the monitor power light on always, while in default timeout the power led blinks) Feb 21, 2017 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .