Suspend to Ram after x minutes of mouse inactivity

Moderator: Forum moderators

Post Reply
User avatar
amethyst
Posts: 2357
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 477 times

Suspend to Ram after x minutes of mouse inactivity

Post by amethyst »

A little tweak I use. You can set the inactivity period to poweroff your machine in Puppy Event Manager (from menu). I don't want to poweroff this way but want to suspend to ram instead. So I searched for the relevant script and changed the poweroff command to my suspend command. In Bionic 32-bit this is /usr/local/pup_event/pup_event_timeout60 (will probably be different for other Puppys). I replaced the wmpoweroff command with my suspend command ie. echo mem > /sys/power/state
I can set the time interval in the Puppy Event Manager (Power tab) the usual way but instead of power down my machine now suspends to ram.

PS: I've made a utility for this and other functions. See my signature for link to nicOS-PowerTimeout utility.

Last edited by amethyst on Wed Jun 01, 2022 4:28 pm, edited 4 times in total.
bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

I had been looking for a way to do this, thanks for the tweak.
An added challenge is now finding a way to avoid the event manager from triggering when one is watching a video (and not moving the mouse in that time) :)

User avatar
amethyst
Posts: 2357
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 477 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by amethyst »

bigpuppyfan wrote: Sun Feb 14, 2021 11:57 pm

I had been looking for a way to do this, thanks for the tweak.
An added challenge is now finding a way to avoid the event manager from triggering when one is watching a video (and not moving the mouse in that time) :)

Easy, set the time interval in puppy event manager to zero (you need to change the time setting again of course if you want to enable suspend/shutdown again).

PS. I made an application for Bionic with regards to suspend/shutdown and the timeout period. Click on the link in my second post on this thread to see how it looks. It shouldn't be too difficult to adjust it for use with another Puppy. We can also make a script so you can toggle between the zero time setting and your desired time setting in puppy event manager. Let me know.

bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

TY - I meant preventing the event manager from triggering when a video is playing, automatically. Similar to caffeine for gnome/ubuntu (I think) https://www.atechtown.com/caffeine-for-ubuntu/

Like the event manager triggering a command (suspend in this case) after x minutes of mouse inactivity IF a specific program is not in fullscreen (as basic users probably wouldn't do the manual steps)

User avatar
amethyst
Posts: 2357
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 477 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by amethyst »

Not sure I quite understand but I did have a look at that link you provided. Are you sure you are not talking just about the blanking of the screen (this works independently from suspend/shutdown)? The latter you can toggle on and off with Screensaver Control in the menu. It will depend whether your video player has a setting for suppressing the screensaver or not. I know VLC has, so in that case you don't need to do anything as the programme will prevent the screen blanking automatically. Note however that if your time-interval setting for suspend/shutdown is shorter than the time your video is running (and you haven't moved the mouse during that time), the system will suspend/shutdown whether screensaver is turned off (or on). In this scenario you should also set the time interval in the puppy event manager to zero.

User avatar
mikewalsh
Moderator
Posts: 5610
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 581 times
Been thanked: 1703 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by mikewalsh »

@amethyst :-

Neat idea, but I'd rather suspend manually. I do so multiple times a day - as a carer you're never quite sure when your "services" will be required! - and I don't always want to go through shutdown/boot and have to open all my apps'n'stuff up again. I long ago modified the 'LogOut GUI' to display a 'Suspend' option in place of the 'Lock Screen' option - which I never use. I also have the 'Pause/Break' button set-up as a one touch 'hotkey' for suspending when I want to do so quickly. (I find this one to actually be easier, 'cos I can switch the mouse 'off' before pressing it.....thus negating the possibility of 'accidentally' turning everything back on again!)

Your proposal, however, shows just how much more versatile Linux - and Puppy in particular - happens to be. The Event Manager/poweroff scripts are highly modifiable, once you know what you're doing. Myself, I have a set of scripts in place to make backups of the 'current' PuppyPin file (/root/Choices/Rox-filer/PuppyPin) which contains all the desktop placement/icon location settings, prior to shutting-down/re-booting.

It doesn't often happen, but I've had the PuppyPin file go AWOL on me, enough times in the past, to make setting these scripts up a worthwhile activity. With the degree of desktop customization I employ, setting everything up again is decidedly time-consuming, and just a complete PITA!

Another script, operated via a custom MenuEntry, allows a "re-set" if, as & when necessary.....replacing the 'old' PuppyPin file and automatically re-starting 'X'.

Another 'custom' MenuEntry automatically runs FixMenus and re-starts 'X' when I manually add .desktop files/icons. And so on.....

Mike. ;)

Puppy "stuff" ~ MORE Puppy "stuff" ~ ....and MORE! :D
_______________________________________________________

Image

User avatar
amethyst
Posts: 2357
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 477 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by amethyst »

mikewalsh wrote: Mon Feb 15, 2021 4:46 pm

@amethyst :-

Neat idea, but I'd rather suspend manually. I do so multiple times a day - as a carer you're never quite sure when your "services" will be required! - and I don't always want to go through shutdown/boot and have to open all my apps'n'stuff up again. I long ago modified the 'LogOut GUI' to display a 'Suspend' option in place of the 'Lock Screen' option - which I never use. I also have the 'Pause/Break' button set-up as a one touch 'hotkey' for suspending when I want to do so quickly. (I find this one to actually be easier, 'cos I can switch the mouse 'off' before pressing it.....thus negating the possibility of 'accidentally' turning everything back on again!)

Your proposal, however, shows just how much more versatile Linux - and Puppy in particular - happens to be. The Event Manager/poweroff scripts are highly modifiable, once you know what you're doing. Myself, I have a set of scripts in place to make backups of the 'current' PuppyPin file (/root/Choices/Rox-filer/PuppyPin) which contains all the desktop placement/icon location settings, prior to shutting-down/re-booting.

It doesn't often happen, but I've had the PuppyPin file go AWOL on me, enough times in the past, to make setting these scripts up a worthwhile activity. With the degree of desktop customization I employ, setting everything up again is decidedly time-consuming, and just a complete PITA!

Another script, operated via a custom MenuEntry, allows a "re-set" if, as & when necessary.....replacing the 'old' PuppyPin file and automatically re-starting 'X'.

Another 'custom' MenuEntry automatically runs FixMenus and re-starts 'X' when I manually add .desktop files/icons. And so on.....

Mike. ;)

OR you can just use my new script, that will make it really easy to toggle suspend/shutdown.

User avatar
mikewalsh
Moderator
Posts: 5610
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 581 times
Been thanked: 1703 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by mikewalsh »

@amethyst :-

I'll take a look at it, Nic. I'll let you know what I think.

Cheers.

Mike. ;)

Puppy "stuff" ~ MORE Puppy "stuff" ~ ....and MORE! :D
_______________________________________________________

Image

bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

amethyst wrote: Mon Feb 15, 2021 1:00 pm

Not sure I quite understand but I did have a look at that link you provided. Are you sure you are not talking just about the blanking of the screen (this works independently from suspend/shutdown)? The latter you can toggle on and off with Screensaver Control in the menu. It will depend whether your video player has a setting for suppressing the screensaver or not. I know VLC has, so in that case you don't need to do anything as the programme will prevent the screen blanking automatically. Note however that if your time-interval setting for suspend/shutdown is shorter than the time your video is running (and you haven't moved the mouse during that time), the system will suspend/shutdown whether screensaver is turned off (or on). In this scenario you should also set the time interval in the puppy event manager to zero.

Sorry, you're right, my post and utility examples were confusing as the link I provided did not explain well what it does. What I meant was:
1- suppose a suspend command is set up after 20 minutes using the above
2- user opens some long video on the browser
3- after 20 mins, the computer would go into suspend in the middle of the video. A basic user would panic

A knowledgeable user could change the event manager like you suggested before opening the video (though would have to remember doing so), but a basic user would probably have no clue of what any of that is.

4-If the event manager (or screensaver script) can detect a specific program is running full-screen (eg palemoon), then it could prevent #3 from happening. I think that is what caffeine achieves (though to prevent a lock screen that seems to happen after x minutes in ubuntu)

Was not aware that VLC could do that, helpful to know. I guess the screensaver script could similarly be adapted to suspend rather than to screen save, though I think it still runs when the browser if fullscreen (for the situation above).

Not sure how windows achieves this, but it doesn't go into suspend whenever a video or music are being played (full-screen or not). Perhaps that is not written in Windows but rather in the programs themselves

User avatar
amethyst
Posts: 2357
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 477 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by amethyst »

Not sure how windows achieves this, but it doesn't go into suspend whenever a video or music are being played (full-screen or not).

This is because Puppy's standard suspend/shutdown scripts only rely on mouse movement for the settings and not computer activity as well. I suggest you try my application (link in the second post). With this one script you can at least quickly and easily toggle between suspend/shutdown/screensaver control options. Alternatively you can start your video player with a script which includes code to disable suspend and the screensaver (you will have to reset your suspend/screensaver settings afterwards though). You can also use a script to exit your video player with added code to restore your preferred suspend/screensaver settings. Personally, I would go with the application as suggested.

HerrBert
Posts: 333
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 17 times
Been thanked: 112 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by HerrBert »

bigpuppyfan wrote: Mon Feb 15, 2021 11:23 am

TY - I meant preventing the event manager from triggering when a video is playing, automatically. Similar to caffeine for gnome/ubuntu (I think) https://www.atechtown.com/caffeine-for-ubuntu/

Like the event manager triggering a command (suspend in this case) after x minutes of mouse inactivity IF a specific program is not in fullscreen (as basic users probably wouldn't do the manual steps)

There is similar code to do this already in puppy. Take a look at /usr/sbin/save2flash

Code: Select all

	RUNPS="`busybox ps`"
	#some apps should not be disturbed by this background stuff...
	if [ "`echo "$RUNPS" | grep -w -E 'make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit|installpkg\.sh|downloadpkgs\.sh|removepreview\.sh'`" != "" ] ; then
		exit 1
	fi

It does not look for programs running in fullscreen, but it can do what you are looking for.
If you reset MOUSECNT to 0 instead of exiting, it would not break the loop.
(Didn't try it yet - just an idea...)

bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

Interesting, and thank you. This one is out of my league, but will do some research and see if I can manage to play with it :)

HerrBert
Posts: 333
Joined: Mon Jul 13, 2020 6:14 pm
Location: Germany, NRW
Has thanked: 17 times
Been thanked: 112 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by HerrBert »

Suggestion:
A standalone script not depending on pup_event

Code: Select all

#!/bin/sh

MOUSECNT=0
POWEROFF=60 # set to your liking
while true; do
	sleep 60
	CURPOS2="`getcurpos`"
	MOUSECNT=$((MOUSECNT+1))
	[ -z "$CURPOS1" ] && CURPOS1="$CURPOS2"
	[ "$CURPOS2" != "$CURPOS1" ] && MOUSECNT=0
	CURPOS1="$CURPOS2"
	if [ $MOUSECNT -ge $POWEROFF ]; then
	 	RUNPS="`busybox ps`"
		if [ "`echo "$RUNPS" | grep -w -E 'make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|smplayer|mpv|vlc|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit'`" != "" ] ; then
			MOUSECNT=0
		else
			/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
			MOUSECNT=0 # in case of resume from suspend
		fi
	fi
done

EDIT

Code: Select all

		if [ "`echo "$RUNPS" | grep -w -E 'make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|smplayer|mpv|vlc|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit'`" != "" ] ; then
			MOUSECNT=0
		else
			/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
			MOUSECNT=0 # in case of resume from suspend
		fi

can be reduced to

Code: Select all

		if [ "`echo "$RUNPS" | grep -w -E 'make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|smplayer|mpv|vlc|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit'`" ] ; then
			/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
		fi
		MOUSECNT=0 # in case of cancel shutdown or resume from suspend
bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

Thanks HerrBert, the script works :thumbup2:
I launched MPV and then ran the script (setting up for 5 seconds) - did not trigger the event until I closed MPV, which is great.

I don't know programming but through trial and error can sometimes manage to make successful edits - found on the web a few scripts to detect whether an application is running full-screen (mostly for when a browser is playing a video), will try to blend it in the next few days.

I've also found, using the above hint by amethyst on how windows does it, a possible solution in a slackware forum, using xscreensaver and a custom script, here:
https://www.linuxquestions.org/question ... page2.html (last post)

https://github.com/montagdude/xscreensaver_autoset
I ran xscreensaver and those custom scripts, seems to work, though something is also not going quite right, as it appears to maybe be only working for 1 cycle and popping notifications every x seconds saying screensaver activated/prevented, etc. Perhaps it can also be adapted to use the standard puppy screensaver instead

User avatar
amethyst
Posts: 2357
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 55 times
Been thanked: 477 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by amethyst »

For general use, I would just start mpv with the following code which will disable screen blanking. Turn screen blanking back on when finished watching videos. Use this code for the defaultvideoplayer script and I would also make it a right-click script.

Code: Select all

xset s off dpms 0 0 0 -dpms && mpv --profile=pseudo-gui "$@"
bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

I've successfully customized HerrBert script (I think), so that the system will suspend after x minutes of mouse inactivity IF an application window is not running full-screen, for instance palemoon playing a dailymotion video [I've used this reference https://askubuntu.com/questions/30147/c ... is-running]

Code: Select all

#!/bin/sh

MOUSECNT=0
POWEROFF=60 # set to your liking
root_geo="$(xwininfo -root | grep geometry)"

while true; do
	sleep 60
	CURPOS2="`getcurpos`"
	MOUSECNT=$((MOUSECNT+1))
	[ -z "$CURPOS1" ] && CURPOS1="$CURPOS2"
	[ "$CURPOS2" != "$CURPOS1" ] && MOUSECNT=0
	CURPOS1="$CURPOS2"
	if [ $MOUSECNT -ge $POWEROFF ]; then
		if [ "$(xwininfo -id $(xdotool getactivewindow) | grep geometry)" = "$root_geo" ] ; then
			MOUSECNT=0
		else
			/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
			MOUSECNT=0 # in case of resume from suspend
		fi
	fi
done

There may be a better way of writing this, but as I don't know programming, I've just tried to understand in a general sense what the command lines mean and then using logic and copy-paste :)

So far it's working. Then I thought of a situation which could be improved. Say the same user opens the video in palemoon but goes away for a while (after pausing it). It would not suspend. The cpu activity script I posted earlier seems unreliable on my testing. So I thought of using the audio as a marker - if a video is playing, then so is the audio, if it's paused, idem. In this case it doesn't even have to be fullscreen (my reference - https://unix.stackexchange.com/question ... is-playing):

Code: Select all

#!/bin/sh

MOUSECNT=0
POWEROFF=60 # set to your liking

while true; do
	sleep 60
	CURPOS2="`getcurpos`"
	MOUSECNT=$((MOUSECNT+1))
	[ -z "$CURPOS1" ] && CURPOS1="$CURPOS2"
	[ "$CURPOS2" != "$CURPOS1" ] && MOUSECNT=0
	CURPOS1="$CURPOS2"
	if [ $MOUSECNT -ge $POWEROFF ]; then
		if grep -q RUNNING /proc/asound/card*/*p/*/status 2>&1  ; then
			MOUSECNT=0
		else
			/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
			MOUSECNT=0 # in case of resume from suspend
		fi
	fi
done

But this one doesn't work very well. Once a process plays any audio, the relevant alsa "flag" in /proc remains at "running" until the browser is closed, even if the video is stopped. I will keep researching for a way to counter this

EDIT: Forgot to mention that, for the fullscreen suspend blocker, it is necessary to install xdotool, which can be found in the puppy package manager (a few KB).
(also, there was an unnecessary line, [RUNPS="`busybox ps`"])

bigpuppyfan
Posts: 74
Joined: Fri Feb 12, 2021 1:20 pm
Has thanked: 12 times
Been thanked: 15 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by bigpuppyfan »

Seems there is no solution by using the audio, since a process locks down in ALSA until it sends a message to release it, so it's down to the programming in the player. For instance, manually pausing a video on youtube does not unlock it, but watching until the end does.

Anyway, another surrogate would be the network activity, and probably a better option too, as I had not thought about a situation where a user leaves the computer downloading something. Most players in websites gradually download the media file as one plays it, not everything at once, so it usually correlates with a video being played or not.

So, I've made a new script. This will suspend/shutdown after x minutes of mouse, keyboard or network inactivity, unless certain applications are running (like kodi). I felt the keyboard part was necessary, as I had the pop-up show when I was writing a document (fortunately just a pop-up, and not immediate shutdown :) )

Code: Select all

#!/bin/ash

POWERTIMEOUT=30 # set to your liking in minutes
IDLE_TIME=$((POWERTIMEOUT*60000)) # Wanted trigger timeout in milliseconds.
TRIGGER=0
while true; do
	sleep 60
	NETUSAGE2="`cat /sys/class/net/*/statistics/rx_bytes | awk '{ SUM += $1} END { print SUM }'`"
	TRIGGER=$((TRIGGER+1))
	[ -z "$NETUSAGE1" ] && NETUSAGE1="$NETUSAGE2"		
	[ "$NETUSAGE2" -gt "$((NETUSAGE1+1024*500))" ] && TRIGGER=0  # set to your liking in KB
	NETUSAGE1="$NETUSAGE2" 
	if [ $TRIGGER -ge $POWERTIMEOUT ]; then
		RUNPS="`busybox ps`"
		if [ "`echo "$RUNPS" | grep -w -E 'kodi|make|cc|gcc|imake|cmake|new2dir|xorriso|xorrecord|xine|gxine|petget|wget|axel|dotpup|mplayer|gmplayer|smplayer|mpv|vlc|gcurl|gimv|burniso2cd|growisofs|cdrecord|pcdripper|xfmedia|xmms|ripoff|pdvdrsab|pburn|mhwaveedit'`" != "" ] ; then
			TRIGGER=0
		else
			idle=$(xprintidle)
			if [ $idle -ge $IDLE_TIME ]; then
			/etc/acpi/actions/acpi_poweroff.sh # don't shutdown without a warning - replace with /etc/acpi/actions/suspend.sh to suspend
			TRIGGER=0 # in case of resume from suspend	
			fi
		fi
	fi
done

This script requires xprintidle, which can be downloaded through the puppy package manager (a few KB in size).

The definition for network inactivity can be adjusted. I've put 500 KB/min as a threshold to avoid false time resets, but can probably go lower.

This script can run on its own, or can work with the puppy event manager. For this, just copy lines 4-25, and paste them in the file /usr/local/pup_event/autoshutdown_daemon, replacing from line 9 onwards in the latter. The advantage of doing this way is that the desired time can be easily adjusted with the puppy event manager (and probably with the nicOS-PowerTimeout).

Personally I also edited acpi_poweroff.sh so that it would suspend as a default, but retaining the warning and option to cancel.

I really enjoyed the challenge of learning more about shell to write this, and is working for me so far in the last few days, but I'm still a complete novice in programming, so any suggestions, shortcomings or potential issues I overlooked would be welcome

EDIT-Had written the wrong lines to be copied into autoshutdown_daemon (should be 4-25, not 3-25, but the forum went down just when I tried to edit it)
EDIT 2-Changed line 8, so that all network interfaces will be taken into account, previously only eth0 was being considered

Last edited by bigpuppyfan on Fri Mar 19, 2021 11:32 pm, edited 2 times in total.
williams2
Posts: 1023
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 288 times

Re: Suspend to Ram after x minutes of mouse inactivity

Post by williams2 »

A simple script:

Code: Select all

#!/bin/sh

# set monitor screen to suspend after 15 minutes of inactivity:
xset dpms 900 0 0

# every 5 minutes, test if the screen is active or not,
# suspend the machine if the monitor screen is off:
while :
do
  xset -q | grep -q "Monitor.*On" || /etc/acpi/actions/suspend.sh
  sleep 5m
done

Note: mpv will disable the Xorg screensaver (xset)
so it should not suspend while mpv is playing.

I have not read all the posts, sorry if this script is a duplicate.

Post Reply

Return to “Tips & Tweaks”