Shutdown Daemon

A typical problem with a home server/NAS used by several people is that someone always turns it on, but who turns it off?  It stays on for days, and the electricity meter whirs happily as the kilowatts get unnecessarily burned…

A better strategy is to let the server shut itself down when it is not being used… intelligently… and this is how I made it happen:

How to determine if the server is not being used

This depends a lot on your setup.  In my case I have a Linux NAS with 6 disks.  One disk (/dev/sda) is the operating system (Linux) disk, and the other 5 are in a (software) raid array serving the shared filesystem which I and my flatmates and I use.  This makes it pretty easy to see if someone is using the system because if none of the 5 raid disks are active, then no one can be using them.  You can see if they are active with hdparm.

A brief introduction to hdparm

hdparm is a really handy linux utility for setting and reading hard disk parameters/status.  To set the spindown time of a disk to 20 minutes:

hdparm -S 240 /dev/sdb

To make your settings persistent, put them into /etc/hdparm.conf

/dev/sdb {
spindown_time = 240

To see the current state (active or not) of a disk:

hdparm -C /dev/sdb

Look up the hdparm documentation for all options.

How to shut down the server…

For this we need a daemon which monitors the hard drive status, and initiates a shutdown when the desired… Introducing: Shutdown Daemon (SDD).

This is a script I put together to do exactly what I described above.  For my setup I have used the following configuration:

log_level: INFO
log_file: /var/log/sdd.log
sleep_before_run: 60
 loop_sleep: 60
 trigger_time: 3600
  - /dev/sdb
  - /dev/sdc
  - /dev/sdd
  - /dev/sde
  - /dev/sdf

This means it waits one minute before starting monitoring, checks the disk status every minute, and will wait for an hour of spun-down disk status before actually shutting down the server (unless one of the disks becomes active again within that time).

Installing SDD

Clone sdd from the github repository:

git clone

Make it:

cd sdd
perl Makefile.PL
make test
make install

Copy the init.d script and config, and update the rc.d entries:

cp examples/etc/sdd.conf /etc/sdd.conf
vim /etc/sdd.conf
cp examples/init.d/sdd /etc/init.d/sdd
update-rc.d sdd defaults
/etc/init.d/sdd start

Saving power

In my case, just configuring the spindown on the hard drives reduces the power consumption of the NAS server from 44 to 28Watt.  Now factor in all those times where the server was forgotten and left on for days… the power saving potential is fantastic!

What other monitors could be useful?

Of course spindown time of the disks isn’t the only condition which could be useful for shutting down your server.  Here’s some others I’ve thought of:

  • hard drive temperature too high
  • No user logged in with ssh (users)
  • No locks on samba file shares (smbstatus -L)

If you have any ideas, feel free to fork the project on github and implement a Monitor – I’m looking forward to seeing many more monitors! 🙂

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

3 Responses to “Shutdown Daemon”

  1. Hello.

    A friendly warning to those who think about changing the spindown time to something shorter:

    A problem with spinning down disks often, is that you risk reducing their life time.

    Spinning disks typically last between 100,000 and 350,000 spindowns, if nothing else fails.

    Spinning down each 20 minutes is not really bad, since that means spinning down up to about 26,000 times per year, but use this with caution.

    Western Digital felt it was necessary to ship this tool:

  2. Hi Jan,
    Good point! I made the same calculation, before I chose 20 minutes, but I don’t expect it to be inactive for 20 minutes, spin down, and then immediately have one single request before the next 20 minutes of inactivity, and that’s the only way which it could reach 26,000 spindowns per year. I expect at most 10 spindowns per day, so ~3650/year, and I think that’s an acceptable load for the power saving.

  3. … and of course you can monitor the number of spin-ups a hard drive has experienced with smartctl (Start_Stop_Count), and keep an eye on that.