LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to suspend after idle or inactivity period without desktop environment or X? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-suspend-after-idle-or-inactivity-period-without-desktop-environment-or-x-4175524123/)

ineloquucius 11-02-2014 04:12 PM

How to suspend after idle or inactivity period without desktop environment or X?
 
xautolock says it's for x-windows--so that's out. Pm-utils will do the shutdown, but damned if I can find a way to set an inactivity timer with it. How is this done? There must be a way...

Beryllos 11-02-2014 11:21 PM

First you need to define what kinds of activity should force the system to keep running. Keystrokes? Commands running in the foreground? Commands running in the background? Commands running via cron?

Next, you need to write a script to detect those activities, determine when the desired period of inactivity has elapsed, and then execute the pm-suspend or pm-hibernate command.

ineloquucius 11-12-2014 06:53 PM

Defining the events that keep the system running is one thing, but what process is running the timer in the first place and ready to sleep if those events don't happen? You mention a script, but is that an efficient and reliable (i.e. elegant) method? Assuming that is the suggestion, how would I run this--rc2.d?

I'm rather surprised that there isn't some apt-get solution out there that just "does" this with a little bit of input on the thresholds and such.

frankbell 11-12-2014 07:54 PM

I think this will help:

http://www.cyberciti.biz/faq/linux-c...op-netbook-pc/

ineloquucius 11-15-2014 04:06 PM

Quote:

Originally Posted by frankbell (Post 5268991)

Thanks Frankbell. But as I say, I'm looking for an inactivity timer--power management same as any other operating system. If I stop using my system, it goes to sleep after x minutes. Not sleep x minutes after I say to sleep, x minutes after I stop using it.

Yes I can wax philosophical about what "stop using" means, but I don't want to. Just like windows or mac or Ubuntu, if I walk away and there aren't certain user applications running, they stop running and go night-night. I'm really confused as to why this is a seemingly exotic topic. I'm not being cranky, I'm just genuinely confused about it.

Beryllos 11-20-2014 01:00 PM

Quote:

Originally Posted by ineloquucius (Post 5268973)
... You mention a script, but is that an efficient and reliable (i.e. elegant) method? Assuming that is the suggestion, how would I run this--rc2.d? ...

I would run the script via crontab. I have read that some users think this is inefficient because the system has to load the program into memory every time, but we are talking about a small program that executes in milliseconds, so I am not worried about it slowing down the system. (If I should worry about this, someone please explain.)

I was thinking about your question because I recently wrote a bash script to analyze the output of ck-list-sessions in order to determine whether any X session is active, and suspend the system after 5 minutes of inactivity, or 90 minutes if lsusb shows that a printer (which is shared) is attached and powered up.

For your question, I had to use the w command. Here is a script which I shall call suspend_after_idle:
Code:

#!/bin/bash
# script to suspend the system if there has been
# no activity on any terminal for the time in minutes
# specified in the first command-line argument $1
#
# Example for 5 minute idle time:
#
# /path_to/suspend_after_idle 5
#

idle_time=$(w -sh | tr -s ' ' | cut -d ' ' -f 4 | tee >(grep -q [0-9]s && echo 0) | (grep -v [a-z] | cut -d ':' -f 1) |  sort -n | head -n 1)
if [ "$idle_time" -ge "$1" ]
then
    (sleep 5; /usr/sbin/pm-suspend) &
fi

It is run by the following line in the crontab of root or another user who is authorized to run the pm-suspend utility:
Code:

* * * * * /path_to/suspend_after_idle 5 > /dev/null 2>&1
It is invoked every minute. Inelegant, maybe, but it works.

The line that extracts the idle time works as follows:
Code:

w -sh | tr -s ' ' | cut -d ' ' -f 4  # cut the idle time column from the output of w
tee >                                # splits the output into two grep-based filters:
(grep -q [0-9]s && echo 0)          # idle times less than 1 minute yield output of 0
(grep -v [a-z] | cut -d ':' -f 1)    # ignore formats of HH:MMmin or DDdays
                                    # and then cut minute field from times in MM:SS format
                                    # outputs of the two filters merge when both are completed
sort -n | head -n 1                  # extract the minimum idle time of all terminals

I included the sleep 5 command and the ampersand, not sure if it is necessary, to allow the script to finish, and hopefully other processes started by crontab, before the suspension is started.

Minor bugs: The script will not suspend the system if any of the following are true:
  • There are no terminals at all
  • The script is executed after all terminals have been idle 1 hour or more
  • The script is executed with a non-integer argument, or no argument at all
The script will not detect activity if a terminal was opened and closed within a single 1 minute interval as clocked by cron.

There may be other bugs or "unintended features."

tassir 12-30-2018 12:57 PM

You can try sspender https://github.com/mountassir/sspender

It allows you to suspend your machine based on pre-defined CPU/Disk usage, and makes sure the machine wakes up at certain times when you need it to be ON.

syg00 12-30-2018 04:12 PM

Generally waking up old, sleeping threads is frowned upon - but seems appropriate in this case ... :p
Looks interesting, and may help others in future via google. I'll see if I can organise a test on Fedora for you.


All times are GMT -5. The time now is 07:34 AM.