2:00am, finals week, what would be a good use of my time? Oh, yeah, how about writing a little bash script to keep my external hard drive from constantly spinning down? Observe:
#!/bin/bash while [ true ] do touch /media/BigEx/.keephdactive sleep 4m done
Wow, right?
Yeah, it’s been kind of bothering me for months, nothing on the internet anywhere had an easy way to do this. And then, after 5-ten minutes of googling in vain all of a sudden I thought to myself: “Hey man, you’re supposed to be a computer scientist, (tbd) can’t you do something about this?” And so, in less time than several failed google searches, I had this.
If you’re on linux and you want to get this running, copy & paste the following into your terminal. It will open up your text editor, inside of which you should change the “BigEx” to the name of your external hard drive. (What it shows up as in the menus and such.) These commands will just put it in your home folder, at the end there’s a command to move it out of there, if you don’t know how.
If you’re in GNOME (eg Ubuntu):
wget http://svn.quodlibetor.com/pubsh/bash/hdactive.sh && gedit hdactive.sh && chmod +rx hdactive.sh
If you’re in KDE (eg Kubuntu, Fedora 9):
wget http://svn.quodlibetor.com/pubsh/bash/hdactive.sh && kate hdactive.sh && chmod +rx hdactive.sh
The only difference between them is a different text editor. (The “&&”s are just ways to join multiple commands together in one line, I did it so that you won’t have to copy/paste 3 times just to get a little hack working :)
Last thing to do then is run it:
./hdactive.sh &
should do it. (The “&” just means “run this process in the background.”)
If you’re in GNOME and you want this file to run every time you start your system, first it’s probably a good idea to put it in your /usr/bin directory:
sudo mv hdactive.sh /usr/bin/
And then you’ll want to add it to your startup session, which is something I don’t know a command for, or how to do it in KDE, so menus time: “System -> Preferences -> Sessions” select “+ add” from the right side, for the name put “HDactive” (or whatever) for the command put “hdactive.sh” (no quotes) and for comment put “oh man quodlibetor’s friggin’ sweet.”
That last part is essential for the proper working of the code :)
OK, it’s now officially been an hour and fifteen minutes since I thought “damn I wish my hard drive would stay on.” On the one hand, that’s way too long, on the other hand, my hard drive has stayed on :)
p.s. I know that there are at least a couple things to do to make this work better (eg accept parameters instead of making people change the code by hand) this works for me, and it’s also damn near my first working bash script ever, so I’ll change it as I have time. I promise that the link will always work though, and if I change it enough that these instructions don’t apply any more I’ll put comments inside it so anybody trying to edit a newer version will see that something’s weird and what to do about it. You can consider it currently licensed under the WTFPL, so do wtf you want to with it :)
[edited a whole bunch of times to get some basic code formatting and syntax highlighting working, since this is my first post with actual code]

{ 4 } Comments
Hey there, I’m an editor working on PC (windows Vista).
Just bought a 1 TB external HD to edit with and I need it to not spin down (sleep)
any chance of this script working for me?
How might I go about it (I’m not a programmer, but I know my way around a PC)
Cheers for any help
-Ferand
Someone just linked to you from HardForum - before they did so I was considering this question and my first solution was nearly the same as yours.
I don’t have this problem myself, but doesn’t `ls` keep the drive spun up? I don’t like creating unnecessary files on the hard-drive, but if you don’t then - if you use `touch` - you have to change the modification date of some other file on the drive. So `ls` seems preferable, unless the o/s is caching the directory listing.
Also, I wouldn’t tend to run the script manually - that way you have to remember to do so every time you reboot your computer. I would omit the `sleep` so that it runs to completion & doesn’t hang around in the background. Then just add it to crontab so that it is automagically run every 4 minutes; I believe this is the crontab syntax:
0-59/4 * * * * ./hdactive.sh
@stroller:
I considered using cron, but I had a lot of trouble setting things to run correctly while I was doing this. However, doing what you describe (starting at the bash command prompt):
The
*/4is equivalent to0-59/4.A couple advantage that I’d say my solution has is that it doesn’t require you to go futzing around with the crontab file, which on some systems you won’t have access to, and also, if the root of that drive is huge doing an
lson it can be a really big deal, way more of a deal than just touching an empty file. If that’s the case you could basically do hdactive in cron:And since hdactive requires significantly more setup and is therefore way more confusing than crontab, if you’ve got access to it, it’s probably the way to go.
I would ask the same question as Ferand did - though I am on Win XP. And, I am not as good as to write this script myself. Is it possible a windows script to be suggested? Thanks!
Post a Comment