# Howto spin down HD when idle?

## finr

Hi all!

My PC is pretty much running all day long, even when I'm not working on it. So I figured my HDs would have a longer life if they were spinned down after eg 30 minutes of inactivity.

The problem is that I don't really know where to start configuring things. I know that hdparm -S should do the job, but I've read a few things in here and most pointed to things like APM and ACPI. ACPI is supported on my machine, and even powering the PC off after shutdown mostly works, but I couldn't figure out how to configure acpid to spin down my HDs, and I even don't know if this is acpid's duty or if hdparm -S should be sufficient...

I'd be grateful if someone could give me a quick explanation as for which tool is the right one to use, and even better, how to use it  :Wink: 

BTW: I'm running the gentoo-dev-sources (2.6.3) here...

TIA!

finr

----------

## adaptr

You can't normally shut down a drive that runs Linux, since the loggers write to it every so often - about once a minute I think.

So not only would you have to configure the HD spindown, but you would have to tweak things so the log files don't get written to more than say once per every few hours.

On a production system this is really not feasible, on a server it is suicide.

Why would you want to do this ?

If the system is truly idle, why not turn it off ?

The average HD doesn't consume much power - in the range of 5-8 Watts max.

And its life may actually be prolonged by not switching it on and off more than you have to.

----------

## finr

 *adaptr wrote:*   

> Why would you want to do this ?
> 
> If the system is truly idle, why not turn it off ?

 

Well, it's more or less idle. It's in my room on campus and most of the time I don't need it, but from time to time I need to access it remotly, so I keep it running.

 *adaptr wrote:*   

> And its life may actually be prolonged by not switching it on and off more than you have to.

 

Hm... I didn't think of that... Kinda makes sense, though  :Wink: 

----------

## primero.gentoo

 *adaptr wrote:*   

>  have to tweak things so the log files don't get written to more than say once per every few hours.
> 
> 

 

This is exactly what i need...  i'm using syslog-ng now, do you know how can i do this???

Bye

Primero

----------

## adaptr

If you Google for it you'll get plenty of pointers.

IIRC it does involve some advanced tweaking to make sure the kernel doesn't log critical or incidental stuff - which you can't normally stop (for very good reasons!)

----------

## davidblewett

Try using hdparm. You can use it to set a hard drive to power off after a certain period of time. I have it set on my 2nd hard drive, that contains my backups and mp3s. Since it's only accessed intermittently, it made sense for me to do that... I'm not sure of the exact switch, try man hdparm.

----------

## SIR

 *davidblewett wrote:*   

> Try using hdparm. You can use it to set a hard drive to power off after a certain period of time. I have it set on my 2nd hard drive, that contains my backups and mp3s. Since it's only accessed intermittently, it made sense for me to do that... I'm not sure of the exact switch, try man hdparm.

 

I think the flag is -S.

----------

## Rainmaker

Why not just use the BIOS settings?

as long as you've configured you're kernel with APM support, this should work

----------

## lightvhawk0

write a script that checks to see if login is running and if its not it would do a hdparm -y /dev/hdXX. and you could use a cron job to make it run every hour.

This would make it so that every hour it sees if you or root is logged in. if your not it will shut down your hdd

----------

## adaptr

 *davidblewett wrote:*   

> Try using hdparm. You can use it to set a hard drive to power off after a certain period of time. I have it set on my 2nd hard drive, that contains my backups and mp3s. Since it's only accessed intermittently, it made sense for me to do that... I'm not sure of the exact switch, try man hdparm.

 

And this is exactly what doesn't work.

The loggers write to the disk every so often, which is not under your direct control - certainly not under the control of hdparm.

All you can achieve with that is to make it spin down temporarily - and have the system spin it up as soon as it wants to write 3 bytes to a log file...

----------

## daemonknight

Hi there,

I work at a Data Recovery company, just to note that it is not always the best thing to shutdown your drive the whole time to extend it's life....the most stress that the drive can go through is when you power it up...if you are needing to have a machine on most of the day then I would definatly advise you to keep it spinning.

Thanks

----------

## steima

OK ... the lifetime of a harddisc would be much higher if it would spin all day and all night.

but fast harddiscs are noisy!

I had the same problem with my little server in my flat. My flat is quite small an especially at night I don't want to hear the hard disc.

So noflushd (eventually in combination with hdparm -S) is the clue. noflushd prevents the flushdeamon to flush every write to disc.

You have to know that systemloggers and so on usually only WRITE to the disc but not read it.

noflushd allows those processes to write to disc but it flushes only if really necessary. If there is a process which reads data from the disc it is spun up and also all unflushed buffers get flushed to disc and of course if the buffer is full the disc is also spun up and everything is flushed to disc.

an emerge noflushd .. installed my noflushd

and you can configure it in /etc/conf.d/noflushd .. as I can remember.

Sometimes noflushd does not spin down the discs ... so you have to set the timeout value for spindown "manualy" with hdparm -S.

I've set the timeoutvalue from hdparm 1 minute higher than from noflushd.

So after 15 mins noflushd prevents funny writting to disc and after 16 mins the harddics is acutally spun down.

As systemlogger I use metalog. It supports buffered writing of the log files anyway so I specified 25 records to be buffered before writing them to disc.

So if I don't use my server a whole day my harddiscs only spin up for doing a backup.

----------

## steima

DO NOT USE NOFLUSHD WITH JOURNALING FILE SYSTEMS

ext2 would do the job

on the one hand a journaling filesystem is prevented to write journal logs ... this is usually a very very bad thing.

on the other hand your harddisc would spin up an down all the time because there are always some blocks read from disc when updating the journal.

and I've read somewhere .. that with one of the journaling fs noflushd doesnt work at all.

matthias

----------

## mlybarger

so what's the conclusion here on how to spin down a non use hdd?  (can't the system itself in general be put to sleep, ie, cache all ram to hdd, and sleep the main system?) how? maybe a guide somewhere... humm....

----------

## steima

as far as i know suspend to ram works quite well with kernel 2.6 systems.

but ... the system is available faster if you just spin down the harddisc. especially for servers it is highly recommended to keep them running  :Wink: 

----------

## mlybarger

do you know where to find a good doc on how to set this up and what is required?  i assume if the system is suspended, then things like cron jobs just don't get executed? how about if remote system suddenly tries to access the nfs mount? does system wake up? maybe this is why my NIC's have a wol connector?  

really, i'm at work all day long most days and the systems sit here on doing  nothing.  heck, most nights they do nothing  :Wink: .  it would be nice if they could suspend w/o having to actually boot up. sure it'll take some time to wake up, but w/o the boot process.

----------

## steima

i did never try suspend to ram functionality but im sure that features like wake on lan would work too.

cron jobs could eventually get executet with the mechanism of acpi alarms. as far as i know these alarms are programmed to a timer component of the acpi subsystem. acpi should wake your computer if a alarm occures ... but dont ask me if that really works!

----------

## odegard

Use the B flag.

```

hdparm -B0 /dev/hda

hdparm -B255 /dev/hda

```

First one is the most aggressive setting turning the drive off almost immediately after access, 255 is the least aggressive. I use this on my old laptop as the drive is very very noisy. It does *not* power up every minute to write to some log.

----------

## Bob P

you can use the "hdparm -Sxxx" parameter to control the elapsed time between drive inactivity and spindown.

----------

## Jerem

```
emerge cpudyn
```

Cpudyn is an alternative to speedfreq or cpufreqd, with the advantage of being able to manage HD spin speeds.

----------

## darklegion

Couldn't you just use tmpfs to redirect logging direclty to ram instead of a hdd,thus eliminating the hdd to spin up every few minutes but still keeping possible critical logs visible?

----------

## Bojan

 *finr wrote:*   

> So I figured my HDs would have a longer life if they were spinned down after eg 30 minutes of inactivity.
> 
> 

 

You can go through lots of trouble and finally make hdparm to spin down the hdd for more than a few minutes (until some logging wakes it again) but I don't thing this will prolong the life of your hard disk  just the opposite holds true: this will make it die sooner. 

Spinning it up constantly will give it much more stress !!!!!

----------

## vonhelmet

The greatest stress on the disk will be during large changes in momentum. It's like cars taking a fair whack of petrol to stop and start again. It takes a lot of energy to get things ticking over in the first place. In the case of a hard disk it will take quite a wedge of energy to get it from 0 to 7200 rpm or whatever in a short time, and that puts a strain on the disk.

Leave it running, or turn the PC off if you're looking to save energy.

----------

## Bojan

I couldn't agree more. Besides  HDD cooling just might be a good idea if you wish to prolong its life.

----------

## Sachankara

echo 1 > /proc/sys/vm/laptop_mode

Lowers the amount of filesystem journal log-writes so the harddrive has time to spin down...

echo 20 > /proc/sys/vm/swappiness

Forces the kernel to use most of the physical RAM before resorting to using the swap...

Both of these two will reduce the disk access...

----------

## wskellenger

 *vonhelmet wrote:*   

> The greatest stress on the disk will be during large changes in momentum. It's like cars taking a fair whack of petrol to stop and start again. It takes a lot of energy to get things ticking over in the first place. In the case of a hard disk it will take quite a wedge of energy to get it from 0 to 7200 rpm or whatever in a short time, and that puts a strain on the disk.
> 
> Leave it running, or turn the PC off if you're looking to save energy.

 

This issue is not that simple.  This has been a long-standing debate among PC users for many years.

Using your automobile analogy, you've neglected the number of miles on the vehicle.  In the same way, the drive has a finite lifetime before it fails due to wearout.

Here is a six year old Western Digital 16GB drive, that was formerly in my Windows desktop machine.  I had Windows automatically suspend the machine after an hour or two of inactivity.  I used to turn the box on in the morning (or at first use) and off when I go to bed.  This drive is in my Linux box now, set to spin down after 20 minutes of inactivity, which it does.  (I don't even have it mounted, so it is almost never accessed)

```
ID# ATTRIBUTE_NAME          FLAG     VALUE WORST THRESH TYPE      UPDATED  WHEN_FAILED RAW_VALUE

  4 Start_Stop_Count        0x0012   098   098   040    Old_age   Always       -       2355

  9 Power_On_Hours          0x0012   090   090   000    Old_age   Always       -       7890

 12 Power_Cycle_Count       0x0012   099   099   000    Old_age   Always       -       1313

```

Notice that the failure threshold for start/stop counts is specified on this old drive as 40 -- actual normalized cycle count (in a normalized value that the mfr determines) is 98 (counts down from 100).

Next candidate is my server drive.  It's a Western Digital 80GB, purchased brand new in about November 2004.  This machine runs 24/7 in my basement as a Jukebox connected to my stereo.  I've also got Apache on it as well as PHPWiki and an issue tracking system intended for my own use.  I tried getting this one to spin down, but after two nights checked the start/stop count and found that it had started/stopped itself many more times than I had intended.  Since then I've disabled starting/stopping until I try the laptop mode as a poster suggested:

```
  4 Start_Stop_Count        0x0032   100   100   000    Old_age   Always       -       220

  9 Power_On_Hours          0x0032   095   095   000    Old_age   Always       -       3723

 12 Power_Cycle_Count       0x0032   100   100   000    Old_age   Always       -       23
```

So this one has been on HALF AS LONG AS MY SIX YEAR OLD DRIVE, because it runs 24/7, but IMO it doesn't need to on this machine that has relatively infrequent use when I'm at work, sleeping, away from home, etc.  Notice high start/stop count as compared to the power cycle count because of my little experiment.

Third candidate is my Linux desktop drive.  Again Western Digital 80GB, purchased new in late 2004 (September?).  

```
  4 Start_Stop_Count        0x0032   100   100   000    Old_age   Always       -       114

  9 Power_On_Hours          0x0032   097   097   000    Old_age   Always       -       2917

 12 Power_Cycle_Count       0x0032   100   100   000    Old_age   Always       -       114
```

So as many times as I've powered up my PC, the disk has been started up as well.  This machine has been left on overnight quite a few times during compiling, but usually I turn it off at night.  

The point is:  After three years you'll see 26,280 hours logged on the drive if it is on 24/7, but very few start/stop cycles.  If you start/stop the drive and can SIGNIFICANTLY reduce the number of running hours (and thus wear) on the drive, why not do it?

The thresholds above are normalized values that are calculated by the manufacturer.  It looks like each about 1000 start/stops is one count, and 750 power on hours is one count (not sure how the values are normalized).  The old drive had a start/stop failure threshold of 40, which means about 60,000 starts/stops are the minimum possible before failure (100-40 = 60, 60 * 1000 = 60k).  At the rate I was going with my desktop machine,  I had about 400 starts/stops per year.  This would equate to 150 years of starting/stopping.  It is difficult to determine how many hours of runtime I've saved on the drive (would need to know machine total ontime), but I'm guessing it's pretty significant.

For more info about the numbers above, try "man smartctl".  I'd be interested to see data from other people's drives as well, with a brief description of how the drive is used (as I did above).

----------

## wskellenger

Do not just blindly set /proc/sys/vm/laptop-mode to a non-zero number.

There are scripts and such which should be included. 

To install necessary scripts:

```
emerge laptop-mode-tools

```

More laptop-mode info is here.

And also check out:  Smart Spindown as a script that will add additional functionality.

I am currently playing with the above scripts on my old PII-200 Jukebox machine.

----------

## wskellenger

I discovered that the value "Power_On_Hours" is the time that the drive is powered up, if it is spinning OR NOT.

So it is difficult to quantify how much runtime you're saving on the drive using just smartctl, as far as I can tell...

The smart_spindown script burps total time the drive spent spun down to stdout, which you can capture in a logfile in order to try to determine how much spintime you're saving...  So far my drive has been shutting down for 7-8 hours at a time, sometimes less than that.  You have to play with the parameters a bit, but you can definitely save some runtime on the motor, lower the temperature inside the case, save some energy, and potentially increase the lifetime of the drive.

----------

## Mandyhan

 *Sachankara wrote:*   

> echo 1 > /proc/sys/vm/laptop_mode
> 
> Lowers the amount of filesystem journal log-writes so the harddrive has time to spin down...
> 
> 

 

Not true. This only causes a full sync once second after every disk activity, so that there remains no dirty data in memory which may cause the disk to spin up later because the kernel wants to write it. Setting this manually is normally useless, because a *lot* of other settings need to be tweaked to actually stop the writes from happening. Use laptop-mode-tools instead. It configures everything for you.

-- Bart Samwel

----------

