1

Inbuilt ACPI feature suspend the Ubuntu pc after specific period. What I want to achieve that in place of suspend it should shutdown the pc completely after predefined idle time. Can anybody help how can I do that. I am able to find shutdown on idle script but these does not allow me to achieve the following feature :

  1. Shutdown on idle after specif period.
  2. Weather a user logged in or not it should shutdown.

Please help can I use inbuilt ACPI function to shutdown on idle and change the default time or there is any other way do do the same?

1 Answer 1

1

You can achieve both things

  1. Shutdown on idle after specif period.
  2. Weather a user logged in or not it should shutdown.

with a simple bash script.

The script will check the idle time and if it is greater than given number it will completely shutdown the system.

You can then set this script to execute every minute or every 10 seconds (as you prefer) by the root cronjob. The root cron run whenever you have logged in or not logged in user as long as your PC is running. So this will solve your problem.

To get the current idle time in miliseconds you can install xprintidle:

sudo apt-get install xprintidle

I have created a simple bash script for you, that you can put to be excuted by the root cron:

#!/bin/bash

export DISPLAY=:0
IDLE_TIME=`xprintidle`
MAX_IDLE_TIME=3600000

if [ "$IDLE_TIME" -ge "$MAX_IDLE_TIME" ]; then
    shutdown -h now
fi

Note that export DISPLAY=:0 is required in order xprintidle to work correctly under cron. MAX_IDLE_TIME is the max idle time in miliseconds after that the computer will be shut down. 3 600 000 miliseconds is equal to one hour. You can change this value by your needs.

So now save this file somewhere on your filesystem (preferably not your home folder, because it may be encrypted). Give the sh file executable premissions:

chown +x file.sh

And then set root cron to execute it every minute:

sudo crontab -e

and put:

* * * * * /path/to/file.sh
2
  • Hi, I have already tried above script. This is working fine if any user is logged into LXDE/Lubutu GUI. But if someone have not logged into the system it is not working as export DISPLAY=:0 does not found in that case. That's why I am looking for any option/script which should work if no one is logged into system like ACPI suspend. Apr 10, 2015 at 11:57
  • @Vijay Singh, the script can be modified to test for failed calls to xprintidle, keeping a counter in a file in /tmp. After 60 failures (for example) you can assume nobody has logged in in one hour and shutdown. (And reset the counter if xprintidle returns some value, to handle the case when an user previously logged logs out).
    – MV.
    Aug 19, 2016 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.