Because the topic abt shutting down PC after certain period of inactivity is redirected to this topic, I will explain this issue here.
I spent lots of time to solve this problem, so I find it useful to share it, to make the same issue simple for others. I hv tried different programmes but they haven't work for me so I found using short script with cronjob the best solution.
I will copy it below and then explain improvements to make it work:
Install xprintidle. This tool gives the idle time of a user.
sudo apt-get install xprintidle
Make a script autoshutdown.sh which checks for the idle time and
instructs the computer to shutdown if idle for 30 minutes.
idle=$(xprintidle)
if [ $idle -gt 1800000 ]; then
shutdown -h now
fi
Make a cronjob for this that checks from time to time if the system
has been idle for too long and if it has been idle for a longer than
30 minutes it will shutdown. Note that the cronjob has to be made for
the root user.
This script needs some improvements to work, like:
idle=`env DISPLAY=:0 su OUR_USER -c xprintidle 2>&1`
OUR_USER is the user we refer to for checking idle time (not root user)
DISPLAY=:0 is correct for one desktop display (run env command to read DISPLAY in your situation)
if script is run by OUR_USER, line above can be reduced:
if script is run by OUR_USER, shutdown command should be preceded by sudo
sudo shutdown -h now
My script was run from cron by line in cron file:
*/5 * * * * /home/OUR_USER/autoshutdown.sh
every 5 minutes
OUR_USER should be replaced as earlier to the user we refer to.
If script is not run by root we should remember to add the line:
ALL ALL=(ALL) NOPASSWD: /sbin/shutdown
in sudoers file, so shutdown command won't need a password to be executed.
I tried such cronjobs on 2 similar distro Lubuntu 12.04.4 RC LXLE 32-bit ( http://www.lxle.net/ )
In one system it works only using root cronjob set in file:
/var/spool/cron/crontabs/root
CAVEAT
Another problem is that xprintidle in my system has given sometimes random for me values and sometimes logically incremental. The final result - my system has been usually shutdown after 20 mins maybe, if I set the max idle value to 30 mins. I think the culprit is xscreensaver which doesn't work as is set by entered parameters.
You can use sleep for that. They're used to delay/ pause an operation. Combine that with shutdown you get a power off timer.
Example to sleep after 1 hour:
sleep 3600 && sudo shutdown now
You can use math as well, here's to power off after 3 hour:
Open a terminal window and type in:
and just replace 60 with whatever number of minutes you want to take.
More info here:
http://ubuntuforums.org/archive/index.php/t-473173.html http://www.linux.org/lessons/beginner/l5/lesson5a.html
Because the topic abt shutting down PC after certain period of inactivity is redirected to this topic, I will explain this issue here.
I spent lots of time to solve this problem, so I find it useful to share it, to make the same issue simple for others. I hv tried different programmes but they haven't work for me so I found using short script with cronjob the best solution.
Firstly I refered to post Timed Shutdown - shutdown after 30 minutes
I will copy it below and then explain improvements to make it work:
This script needs some improvements to work, like:
OUR_USER
is the user we refer to for checking idle time (not root user)DISPLAY=:0
is correct for one desktop display (runenv
command to readDISPLAY
in your situation)if script is run by
OUR_USER
, line above can be reduced:This topic is described http://ubuntuforums.org/showthread.php?t=1069602
if script is run by
OUR_USER
,shutdown
command should be preceded bysudo
My script was run from cron by line in cron file:
If script is not run by root we should remember to add the line:
in sudoers file, so shutdown command won't need a password to be executed.
I tried such cronjobs on 2 similar distro Lubuntu 12.04.4 RC LXLE 32-bit ( http://www.lxle.net/ )
In one system it works only using root cronjob set in file:
/var/spool/cron/crontabs/root
CAVEAT
Another problem is that xprintidle in my system has given sometimes random for me values and sometimes logically incremental. The final result - my system has been usually shutdown after 20 mins maybe, if I set the max idle value to 30 mins. I think the culprit is xscreensaver which doesn't work as is set by entered parameters.
You can use
ComplexShutdown https://launchpad.net/complexshutdown
or EasyShutdown https://launchpad.net/easyshutdown
You can use
sleep
for that. They're used to delay/ pause an operation. Combine that withshutdown
you get a power off timer. Example to sleep after 1 hour:You can use math as well, here's to power off after 3 hour: