LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-02-2014, 04:12 PM   #1
ineloquucius
LQ Newbie
 
Registered: Jan 2010
Posts: 27

Rep: Reputation: 0
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...
 
Old 11-02-2014, 11:21 PM   #2
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 327

Rep: Reputation: 125Reputation: 125
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.
 
Old 11-12-2014, 06:53 PM   #3
ineloquucius
LQ Newbie
 
Registered: Jan 2010
Posts: 27

Original Poster
Rep: Reputation: 0
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.

Last edited by ineloquucius; 11-12-2014 at 06:59 PM.
 
Old 11-12-2014, 07:54 PM   #4
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mageia, and whatever VMs I happen to be playing with
Posts: 12,131
Blog Entries: 14

Rep: Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067Reputation: 3067
I think this will help:

http://www.cyberciti.biz/faq/linux-c...op-netbook-pc/
 
Old 11-15-2014, 04:06 PM   #5
ineloquucius
LQ Newbie
 
Registered: Jan 2010
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by frankbell View Post
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.
 
Old 11-20-2014, 01:00 PM   #6
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 327

Rep: Reputation: 125Reputation: 125
Quote:
Originally Posted by ineloquucius View Post
... 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."

Last edited by Beryllos; 11-20-2014 at 03:57 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Screen saver does not start after period of inactivity statguy Slackware 3 02-27-2009 07:52 PM
Blank Screen after short period of inactivity SweetLou Debian 6 12-13-2006 08:45 PM
Centos - console hangs after inactivity period yuri_d Red Hat 3 09-15-2006 07:52 PM
How do I automatically logout after a period of inactivity? AshleyK Linux - Security 4 12-31-2004 10:47 PM
Log entry during period of inactivity Gavin Linux - General 1 01-14-2002 01:08 AM


All times are GMT -5. The time now is 07:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration