Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like my home GNU/Linux server to power down after 30 minutes of inactivity. For "activity", I'm thinking

  1. any new [CUPS] print jobs
  2. any requests to a locally running Apache server
  3. any activity over SSH except for failed logins

If any of these things occur, I'd like the 30-minute countdown to reset.

The first two seem pretty doable. I could just write a little long-running Python or Bash program that

  1. waits patiently for 30 minutes to issue shutdown -h now
  2. monitors the filesize of CUPS and Apache access logs, resetting the timer when any file growth is detected

But I'm not really sure about keeping track of SSH activity. Is there a somewhat easy way to do that? I'm not really interested in dealing with libpcap or whatever, unless it is pretty straightforward.

Another thought that strikes me is that this might be generically useful, so maybe I'll make something extensible and release it as Free Software.

I'm not sure if it is relevant, but the server in question is running Ubuntu 10.04.4 LTS.

My question is similar to this one, except for the activity detection.

share|improve this question

closed as off topic by John Gardeniers, EEAA, Scott Pack, HopelessN00b, rnxrx Aug 25 '12 at 5:58

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Out of curiosity, why are you doing this? What if a request comes in after 30 minutes and the server is shutdown? – Chida Aug 13 '12 at 4:30
Such a server would be useless in a business environment so I'm gong to assume this is for your home system and vote accordingly. – John Gardeniers Aug 13 '12 at 5:18
why power efficiency can't be useful in a business environment? – Giovanni Toraldo Aug 13 '12 at 7:48
Just because this does not make sense in the business / professional environments you know, it does not mean this behaviour is generally undesirable. I've had several test boxes where this would have come in handy. Additionally, I think it's an interesting question because of the required insight into *nix. OK, perhaps unix.SE would be a better place to ask, but I think the question is perfectly ok here. – jstarek Aug 13 '12 at 8:12
Yes, this is a home server. Updated title and body. – Adam Monsen Aug 14 '12 at 0:37

PowerNap is a project which provides this kind of functionality. Unfortunately the conditions are not easily extensible, and so if the conditions it provides do not suit your requirements you either need to hack the code or find something else.

share|improve this answer
Thank you, PowerNap looks cool! – Adam Monsen Aug 14 '12 at 16:56

Do not write a long-running program. Use cron to run a script every five minutes or so to create a file that is half an hour old (use the touch command) and then checks whether the various log files are newer than that reference file (using the test command).

Perhaps in your system, sshd logs (via syslog) to the /var/log/messages file. This will not work for your purpose, as many other things log there too, so you might need to edit /etc/syslog.conf so that sshd also logs to dedicated log files that you can monitor.

You should probably also check if anyone is logged in before you power down. (And depending what you use ssh for, this might be enough for ssh too, in which case you will not need to worry about sshd logs.)

I'd be surprised if anyone else finds this useful.

share|improve this answer
Thanks! I like this. And I just thought of a way to check SSH (or console login) activity: w. It shows idle time. I'll probably DIY PowerNap isn't helpful. – Adam Monsen Aug 14 '12 at 16:55

You can use netstat -p to look for connections associated with sshd (if run under sufficient privilege level, like root), and there is a diagnostics page for Apache (enabled by mod_status) which can tell you if the server is doing something (you can strip logging from access to the page and lock it down to requests from localhost only). I believe CUPS also has an HTTP-accessible diagnostics, like a list of active jobs (or just just use lpq -a).

share|improve this answer

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