LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Suspend on inactivity (https://www.linuxquestions.org/questions/slackware-14/suspend-on-inactivity-4175604195/)

askfor 04-19-2017 03:36 PM

Suspend on inactivity
 
How do I make Slackware suspend on inactivity ? I know pm-suspend and similar commands and I can suspend when I want. However, how to make computer suspend itself after, let's say, 10 minutes of inactivity. How the "inactivity" is defined and how it can be determined ? How to make it safe, so computer would not be interrupted while doing something, like compiling code ?

I am using lightweight IceWM window manager, so KDE or XFCE or something "magic tricks" do not qualify as answers.

Didier Spaier 04-19-2017 05:40 PM

See this old post.

Man xorg.conf for the details

askfor 04-19-2017 06:54 PM

That was about monitor. I mean suspend the whole machine. pm-suspend does that. The problem is how to detect that machine is idle. I suspect it might be something about acpid. It has hooks for machine states, but they seem to handle switch button actions.

qweasd 04-19-2017 08:14 PM

You should define inactivity. There's keyboard inactivity & mouse inactivity, is that good? Does for example watching a 1-hr long movie with mplayer count as 50 minutes of inactivity?

askfor 04-19-2017 08:40 PM

NO, absolutely not. Watching movie is activity. I don't want PC to suspend in the middle of the movie. Also, I don't want it to stop building a large Slackbuild or something. I know Windows XP could do it. Later versions too, probably, although I have never seen it. Mac too, probably. I remember that Windows XP could define what would shutdown (screen, disk, everything) after what amount of inactivity. However there was no way to define "inactivity". I have no idea what would Windows or Mac do if compiling large number of source files for couple of hours. Never tried it.

I've seen a script which processes output of 'w' command, but that works for terminal sessions only.

Keyboard & mouse inactivity is definitely a major condition.

Screen inactivity is also a good condition, especially combined with mouse and keyboard.

How to define a CPU inactivity ? There is always something going on. 'ps' and 'top' in batch mode ? There is always something going on. Highest CPU percentage lower than some threshold ? What if process is niced ?

Gordie 04-19-2017 08:40 PM

Perhaps the power moniter? I see something like that in the settings, on KDE anyway

rob.rice 04-19-2017 09:35 PM

Quote:

Originally Posted by askfor (Post 5699504)
NO, absolutely not. Watching movie is activity. I don't want PC to suspend in the middle of the movie. Also, I don't want it to stop building a large Slackbuild or something. I know Windows XP could do it. Later versions too, probably, although I have never seen it. Mac too, probably. I remember that Windows XP could define what would shutdown (screen, disk, everything) after what amount of inactivity. However there was no way to define "inactivity". I have no idea what would Windows or Mac do if compiling large number of source files for couple of hours. Never tried it.

I've seen a script which processes output of 'w' command, but that works for terminal sessions only.

Keyboard & mouse inactivity is definitely a major condition.

Screen inactivity is also a good condition, especially combined with mouse and keyboard.

How to define a CPU inactivity ? There is always something going on. 'ps' and 'top' in batch mode ? There is always something going on. Highest CPU percentage lower than some threshold ? What if process is niced ?

the easiest way would be to go into your desk top environment setting manager and turn blanking off manually then restart it manually when your build or move is done
the power manager has no way to tell your building something or watching a video all it can see is the keyboard or mouse

montagdude 04-19-2017 10:05 PM

No, you do not need to use a fancy desktop environment to suspend after inactivity. You can do it with xscreensaver. Here is a script that I run on startup (with reference in the comments):

Code:

#!/bin/bash

# Suspends laptop 2 minutes after screensaver kicks in (as long as the
# screensaver is still running at that point).
# Template from arch linux forums:
# https://bbs.archlinux.org/viewtopic.php?id=147773

PAUSE=2m

function process ()
{
  while read INPUT
  do
    case "$INPUT" in
      BLANK*)
        # Wait PAUSE and check to make sure it is blanked
        sleep $PAUSE
        local ISBLANKED=$(xscreensaver-command -time | grep ' blanked')
        if [ "$ISBLANKED" != "" ]; then
          user_suspend
        fi
        ;;
    esac
  done
}

xscreensaver-command -watch | process

user_suspend command:

Code:

#!/bin/bash

dbus-send \
  --system \
  --dest=org.freedesktop.UPower \
  --type=method_call \
  --print-reply \
  /org/freedesktop/UPower \
  org.freedesktop.UPower.Suspend

The basic idea is that the xscreensaver-watch sends status information to the process function. When the screen is blanked, this function waits 2 more minutes and then suspends the computer. So you need to set up xscreensaver to blank the screen if you want to use this verbatim. Otherwise, of course, you can modify the script to your liking.

I also have a script to change xscreensaver settings for whether I'm watching a movie (disable xscreensaver) or not (enable it). I bind them to a hotkey so the setting can be quickly changed based on what I'm doing.

bassmadrigal 04-19-2017 10:06 PM

I don't know of anything that will detect if your computer is doing something in the background like compiling.

The closest I can provide you would be a script I created to mute my sound when the screen went into standby (to prevent notifications from sounding at all hours of the night). You could modify it so that when it detects the screen has been put in standby, after a certain period of time (and probably checking to verify the screen is still off), it could run the pm-suspend command.

https://github.com/bassmadrigal/scri...e_screenoff.sh

Maybe you could also add to the script to check the load average, and if it is high, prevent suspending the computer. But I don't think you're going to find anything ready-made. It will likely be a script that you need to create and run in the background to detect various things and then run the commands you want.

EDIT: montagdude, where were you when I created my script? :P That seems much more elegant than my solution. Although, I have the screensaver disabled, so it seems it wouldn't have worked without me enabling it.

montagdude 04-19-2017 10:18 PM

Quote:

Originally Posted by bassmadrigal (Post 5699531)
EDIT: montagdude, where were you when I created my script? :P That seems much more elegant than my solution. Although, I have the screensaver disabled, so it seems it wouldn't have worked without me enabling it.

EDIT: I can't take too much credit for the script. I found the basic concept on the Arch forums after searching and just modified it a little bit.

xscreensaver is mainly useful because it keeps track of mouse and keyboard activity. I don't think there's a way to make it automatically know when you're watching a movie or compiling something, but it's easy enough to change its settings programmatically. Here's the script I use for that, which just makes some automated changes to the ~/.xscreensaver file. This one may need to be tailored to the user's preferences, so consider it an example only.

Code:

#!/bin/bash

function print_usage()
{
  echo "Usage: xscreensaver_controller OPTION"
  echo "Options:"
  echo "  --powersave-off: Deactivate screensaver and dpms power management"
  echo "  --powersave-on: Activate screensaver and dpms power management"
}

MODE=$1
if [ $# -ne 1 ]; then
  print_usage
elif [ "$MODE" == "--powersave-off" ]; then
  sed -i 's/mode:\t\trandom/mode:\t\toff/' ~/.xscreensaver
  sed -i 's/dpmsEnabled:\tTrue/dpmsEnabled:\tFalse/' ~/.xscreensaver
elif [ "$MODE" == "--powersave-on" ]; then
  sed -i 's/mode:\t\toff/mode:\t\trandom/' ~/.xscreensaver
  sed -i 's/dpmsEnabled:\tFalse/dpmsEnabled:\tTrue/' ~/.xscreensaver
else
  print_usage
fi


qweasd 04-19-2017 10:35 PM

Here's what I could dig up for general scripting, without having to activate xscreensaver:

https://anonscm.debian.org/cgit/coll...idle.git/tree/ has the C code for detecting idle keyboard & mouse time which should work, since I can see the requisite includes. It's like literally several lines of C.

https://stackoverflow.com/questions/...ivity-in-linux makes a reference to a perl module which allows to get a similar result.

Now you can write a shell script that will periodically (like once a minute) check the idle time and suspend via DBUS after it goes over a cap.

For taking care of videos, just ps ax | grep [m]player && echo mplayer is running, or along these lines.

The rest of "inactivity" definitions are too subjective, so for things like a big build, you will almost certainly have to rig up something special.

Gordie 04-19-2017 10:45 PM

1 Attachment(s)
Quote:

Originally Posted by askfor (Post 5699504)
NO, absolutely not. Watching movie is activity. I don't want PC to suspend in the middle of the movie. Also, I don't want it to stop building a large Slackbuild or something. I know Windows XP could do it. Later versions too, probably, although I have never seen it. Mac too, probably. I remember that Windows XP could define what would shutdown (screen, disk, everything) after what amount of inactivity. However there was no way to define "inactivity". I have no idea what would Windows or Mac do if compiling large number of source files for couple of hours. Never tried it.

I've seen a script which processes output of 'w' command, but that works for terminal sessions only.

Keyboard & mouse inactivity is definitely a major condition.

Screen inactivity is also a good condition, especially combined with mouse and keyboard.

How to define a CPU inactivity ? There is always something going on. 'ps' and 'top' in batch mode ? There is always something going on. Highest CPU percentage lower than some threshold ? What if process is niced ?

Hmm, we both posted at the exact same time so I didn't see this information. It changes things. I have Cairo-Dock installed. One function is to disable the screensaver. I use that when watching videos. Not sure if that would help you but ...

qweasd 04-19-2017 11:19 PM

A fun fact, very related: my wife watches a lot of youtube in web browser, and there seems to be no bloody way to detect THAT activity. She uses KDE, but can KDE detect html5 video playing? I remember hitting a brick wall on that one :) I ended up setting her up with 2-hour suspend policy, enough to take care of most web videos. Even though it's fun to look things up, you can see above, I haven't really tested these ways, since I personally waste my time on getting my laptop to never initiate anything power-related at all :) My suspend, for example, is hooked to Tux+s, which is trivial to do in pretty much any window manager.

montagdude 04-19-2017 11:51 PM

Quote:

Originally Posted by bassmadrigal (Post 5699531)
Maybe you could also add to the script to check the load average, and if it is high, prevent suspending the computer. But I don't think you're going to find anything ready-made.

Actually, now that I think about it, checking the average CPU load periodically in a script would probably be a good method to automatically enable or disable the screensaver. It should work for watching movies, listening to music, or compiling, as long as you can tweak the threshold correctly. Thanks for giving me a new idea to tinker with! :)

askfor 04-20-2017 08:43 AM

I did search on how KDE does power management. It looks like there is a daemon which is watching actual power consumption, and it triggers events. User can configure responses to those events, including "suspend session". One article suggests that there exist other deamons which do similar tasks.

So, the key seems to be actual power consumption by the system. There is a program called powertop, which is installed by default. I am looking into it.


All times are GMT -5. The time now is 06:26 PM.