? Editing: Post:21.body Save Delete Cancel
Content changed Sign & Publish new content

The Blinking Prompt Times █

Zite Index/Best Of
Latest znqa release : 0.2.0
I'm on ZeroMe
Feel free to propose a Chinese translation !
寻找这篇文章的翻译!
谢谢!
Best viewed in Dark Mode™, like anything else really :)

Follow in NewsfeedFollowing

Latest comments:

Add new post

Title

21 hours ago · 2 min read ·
3 comments
Body
Read more

Not found

A simple script to automatically put your Linux computer to sleep

on Feb 09, 2018

I like to have my computer sleep to RAM under certain conditions, and of course being the geek I am, I want full control over those.

Enter this small script which will put your computer to sleep if idle since > N minutes (using xprintidle) AND if some applications are not running (You would not want your computer to go to sleep while you use it to listen to music, are downloading something or are doing a backup)

Of course it doesn't cover all cases (What to do about the browser ?) but I believe it's still a pretty good basis

#!/bin/bash

function sleeptime()
{
check vlc &&
check mpv &&
check transmission &&
check virt-manager &&
check xfsdump &&
check emerge &&
sudo pm-suspend

sleep $check_interval # After sleep, don't check before N minutes
}

function check()
{
pgrep $1
if [ $? != 0 ]
then
printf "not using $1\n"
else
printf "using $1, skipping sleep\n"
return 1
fi
}

# Base variables
max_idle=600000
check_interval=30

# Handle options
while test $# -gt 0; do
case "$1" in
--test)
max_idle=600;
check_interval=3;
break
;;
*)
break
;;
esac
done

# Start
while [ 1 ]
do
current_idle=$(xprintidle)
if [ $current_idle -gt $max_idle ] # 10 mins
then
printf "Computer has been idle for 10 minute\n"
sleeptime
fi
sleep $check_interval # Check every 30 seconds
done

2 Comments:

user_name1 day ago
Reply
Body
jamesholdenon Feb 10, 2018
Reply

ssdifnskdjfnsdjk: I was always worried about automated sleep, not to interrupt something important in future. Maybe checking CPU, HDD activity before sleep can help.

Good idea, adding the load average in the last 15 minutes in the max adds some more info about the "state" we're in !

binchan2on Feb 10, 2018
Reply

Never thought about this because my Linux laptop is always plugged XD Looks helpful for battery life.

This page is a snapshot of ZeroNet. Start your own ZeroNet for complete experience. Learn More