Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Automatic Idle Shutdown
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
sleepy boy
n00b
n00b


Joined: 03 May 2006
Posts: 29
Location: Bruges, Belgium, earth

PostPosted: Thu Nov 01, 2007 8:37 pm    Post subject: Automatic Idle Shutdown Reply with quote

This is a simple bash script that I wrote in a hurry, b/c I ALWAYS forget to shutdown my system and I'm tired of that:( AND it does save quite few bucks on the energy bill :)

Its rather simple, but very effective :)

If u find it interesting, have some ideas, improvements, pls say so :D

Basic idea:
1) if the system is idle for more then 2 hours, it needs to shutdown automatically.
2) if the computer is working on something(emerging, ...) it must NOT shutdown
3) it must warn me before actually shutting down

UPDATE: the script can now be configured NOT to shutdown when certain programs are running(mplayer, ....)

check requirements to to see what it needs to run

You can easily modify it to your own needs since its quite basic :)


Code:
#!/bin/bash

###REQUIREMENTS:
# download, compile and install xprintidle,
#available from "http://www.dtek.chalmers.se/~henoch/text/xprintidle.html"
# install gxmessage

# the user running this script must have sudo access to the "shutdown" command without passwd
# u can do this by adding "%wheel ALL=(ALL) NOPASSWD: /sbin/shutdown" for gentoo
# change "wheel" to "users" if the user doesn't have wheel access

###VARIABLES

## Settings
#required idletime in milliseconds before shutting down(1hr=3600000ms)
reqidletime=3600000

#maximum 1 minute load average for shutdown (scales from 1-32...)
#(This needs some work,it doesn't work behind the comma, and is
#interpreted as a integer atm)
maxloadavg=1

#programs that should prohibit shutdown
programs="mplayer ktorrent emerge cc"



##Needed Variables
#measures the current loadavg
loadavg=0
#the current measured idletime
idletime=0
#variable that lists each seperate program in the $programs array
each=""

## Status Flags(important internal variables)
#flag to check if the load is too high or not, 1 means too high
loadstatus=0
#flag to check if the idletime needed to shutdown is aqcuired, 1 means shutdown
idleack=0



### SHUTDOWN CHECK LOOP
#check if idletime is acquired, if it is, just quit(system will aready be shutting down)
while [ $idleack -lt 1 ] ; do
   # sleep time( this script only needs to run once in a while ofcourse :)   
   sleep 30
   
   #check the current idletime(in milliseconds)
   idletime=$(xprintidle)
   #setting $idleack flag, depending on the current idletime
   if [ $idletime -gt $reqidletime ]; then
      idleack=1
   else
      idleack=0
   fi
   # Starting shutdown check
   if [ $idleack = 1 ]; then
      #check the current 1 min loadaverage
      loadavg=$(cat /proc/loadavg | cut -c 1)
      #checking if 1min loadavg is exceeding max loadavg allowed
      if [ $loadavg -lt $maxloadavg ]; then
         #setting status to allowed
         loadstatus=0
      else
         #setting status to disallowed to reboot
         loadstatus=1
      fi

      #checking if the load is ok
      if [ $loadstatus = 0 ]; then
            #running prohibition program check for each program
            for each in $programs ; do
               progcheck=$(ps aux | grep -i $each | wc -l)
               if [ $progcheck = "2" ]; then
                  idleack=0
               fi
            done
         if [ $idleack = 1 ]; then
            check=$(gxmessage -center -print -timeout 10 -buttons "CANCEL:1" The system has been idle to for $idletime ms and will shutdown in 60 seconds, press cancel to abort)
            if [ "$check" = "CANCEL" ]; then
               #reset idleack to fix the loop(if it remains to 1, the loop will exit)
               idleack=0
            else
               sudo shutdown -h now
            fi
         fi
      fi
   fi
done

watch out for the split line at "gxmessage":)
_________________
To be or not to be, it's not a question. It's a choice :P


Last edited by sleepy boy on Mon Nov 05, 2007 11:46 pm; edited 3 times in total
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1569
Location: U.S.A.

PostPosted: Sun Nov 04, 2007 10:15 pm    Post subject: Reply with quote

Nice work! Very readable.

How are you running it, as a cron script (hourly, or every 10, etc.?)
Back to top
View user's profile Send private message
sleepy boy
n00b
n00b


Joined: 03 May 2006
Posts: 29
Location: Bruges, Belgium, earth

PostPosted: Mon Nov 05, 2007 4:59 pm    Post subject: Reply with quote

Thanks :)

I execute it at the start of my fluxbox session (~/.fluxbox/startup), guess u can do this with about any wm...

it doesnt need to run in cron as there is a sleep timer implemented:)
Code:

   ### sleep time
   sleep 30


so it runs every 30 seconds,

u could ofcourse change it to a cronjob, this is what you'dd need to change:

1) u can remove the while loop
2) DISPLAY needs to be set, so it knows what X server to look on
3) xprintidle needs to be run as the user thats running the X session, so you would need something like:

Code:
su - USER -c "DISPLAY=:0 xprintidle"



I'm also thinking about adding an application check into it, for e.g. mplayer,gcc,emerge,... :) and I'd like to change the load average check in something more "exact"
_________________
To be or not to be, it's not a question. It's a choice :P
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1569
Location: U.S.A.

PostPosted: Mon Nov 05, 2007 7:57 pm    Post subject: Reply with quote

Yes, of course -- I looked right at the while loop and sleep statement, but it didn't sink in.
: )

Thanks for sharing it.
Back to top
View user's profile Send private message
sleepy boy
n00b
n00b


Joined: 03 May 2006
Posts: 29
Location: Bruges, Belgium, earth

PostPosted: Mon Nov 05, 2007 11:03 pm    Post subject: Reply with quote

Hey, I'm happy someone except me has found a good use for it:D

Still, if u, or someone else got ideas or suggestions, I'm willing to implement them if they're usefull !!!

The script has been cleaned up, and made a bit faster, there were some unnecessary checks while the idle time was "low", they have been moved to only execute when the max idle time was reached(so it would take even less processing power :))

I'm thinking about posting my backup script on the forums for everyone, but its +-700 lines... without the config file :)
_________________
To be or not to be, it's not a question. It's a choice :P


Last edited by sleepy boy on Tue Nov 06, 2007 12:04 am; edited 1 time in total
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1569
Location: U.S.A.

PostPosted: Mon Nov 05, 2007 11:52 pm    Post subject: Reply with quote

sleepy boy wrote:
I'm thinking about posting my backup script on the forums for everyone, but its +-700 lines... without the config file :)

You should post it. Or you could put somewhere else and post a link to it.

I for one would like to see it. My backup script is just a basic wrapper for rsnapshot (which is fundamentally a backup script itself based on rsync). My script just makes it convenient for me to initiate backup from different crontab entries with different options (e.g., hourly sync, daily rotate, weekly rotate fsck, monthly rotate) and it handles the external media. But I'm only backing up /etc and a few key configuration items.

So, yeah, post it. I think the more people share stuff the more everybody benefits. I've received some good suggestions from people when I've done it.
Back to top
View user's profile Send private message
sleepy boy
n00b
n00b


Joined: 03 May 2006
Posts: 29
Location: Bruges, Belgium, earth

PostPosted: Tue Nov 06, 2007 1:05 am    Post subject: Reply with quote

lol, I'm using linux for 7 years, and I still can't figure out how to use rsync(d) right :cry:
Can u give me the link to your backup script??? It sounds a LOT better designed then mine...
I can barely make any sense out of my own script anymore(wrote it 3 years ago, without comments or any explanation, but with a shitload of functions :roll: ) , so with rsync, I'd try to rewrite it BETTER, shorter, more efficiently. with network support :)

Things that I'd implement (again):
backing up selected dirs
backing up selected mysql db's(local AND remote)
backing up incremental or full/daily or weekly(grandad stragegy...or whatever english speaking ppl call it...)
backing up remote webserver data + remote mysql data(optional)
_________________
To be or not to be, it's not a question. It's a choice :P
Back to top
View user's profile Send private message
Bones McCracker
Veteran
Veteran


Joined: 14 Mar 2006
Posts: 1569
Location: U.S.A.

PostPosted: Tue Nov 06, 2007 7:57 am    Post subject: Reply with quote

Here's my backup script (it really just makes up for a few missing requirements for rsnapshot):
http://forums.gentoo.org/viewtopic-p-4465583.html#4465583

But from what you've listed, it would seem to meet your needs.
Back to top
View user's profile Send private message
CptFastbreak
n00b
n00b


Joined: 04 Sep 2007
Posts: 16

PostPosted: Fri Nov 16, 2007 11:34 am    Post subject: Reply with quote

I really like that script you wrote - I am running a Computer room at my University, and it bugs me that users will frequently forget to log out. I am going to adapt your script for automatic user kicking after a specified idle time. To facilitate install, I added an ebuild for xprintidle:
http://bugs.gentoo.org/show_bug.cgi?id=199348
Back to top
View user's profile Send private message
dermund
Apprentice
Apprentice


Joined: 28 Aug 2007
Posts: 205
Location: Sprawl

PostPosted: Tue Nov 20, 2007 10:08 pm    Post subject: Reply with quote

Nice work! I'm working on something similar to that atm. Just if you want to get rid of xprintidle - "who -u" also gives you the idle time for all users. It gives you a '.' if the user was not idle for less than a minute.
have fun!
Back to top
View user's profile Send private message
PraetorZero
Apprentice
Apprentice


Joined: 11 Dec 2004
Posts: 239
Location: /home

PostPosted: Wed Jul 23, 2008 2:45 pm    Post subject: Reply with quote

Nice script! I'll be implimenting this in a while. :)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum