1

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.

5
  • 1
    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, 2012 at 4:30
  • 1
    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. Aug 13, 2012 at 5:18
  • why power efficiency can't be useful in a business environment? Aug 13, 2012 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, 2012 at 8:12
  • Yes, this is a home server. Updated title and body. Aug 14, 2012 at 0:37

3 Answers 3

3

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.

1
  • 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. Aug 14, 2012 at 16:55
2

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.

0
0

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).

Not the answer you're looking for? Browse other questions tagged .