Automatically suspend file server after inactivity?

dighn

Lifer
Aug 12, 2001
22,820
4
81
I built a file server using debian, and while it doesn't use much power (Via Eden board), I would still like to shut down the HDDs after long periods of inactivity. Sometimes it may be a few days without me accessing anything on there, so why keep it on? Especially since I've read that consumer grade HDDs aren't meant to be running 24/7.

I first considered spinning down the HDs, but that seemed more trouble than its worth with the write buffering adjustments and such.

Suspend seemed a viable option, with Wake-on-Lan, but it doesn't look like Linux easily supports automatic suspension. I suppose I could script something to monitor samba activity, but is there something already available that does this?

Thanks in advance
 

Kakumba

Senior member
Mar 13, 2006
610
0
0
Heres my script:

#!/bin/bash

if [ -f restart.lock ]
then
date
echo "Restart aborted, restart lock file in place"
exit 1
fi

count=0

smbstatus > info.txt
grep "No locked files" info.txt > /dev/null

while [ "$?" != "0" ]
do
date #debug purposes, see if it actually is running
echo $count
echo "Locked files, waiting"
sleep 900
count=`expr $count + 1`

if [ $count = 8 ]
then
date
echo "Restart aborted, too many attempts with locked files"
exit 2
fi
smbstatus > info.txt
grep "No locked files" info.txt > /dev/null
done

date
echo "Shutting down as per auto-shutdown script"
shutdown -h now
exit 0

I simply run that every day at midnight, on a cron job. It first checks for a file call restart.lock, so if I want to avoid shutdowns for a while, just create that file in teh same directory as the shell script. Then it just checks if samba reports any locked files, and if not, shuts down. If so, it waits 15 minutes then checks again. ties 8 times, if still locked files, then it stops trying. You might think of some way to make it cleaner, that was just something I bashed up in a couple of minutes, quick and dirty.

Hope that helps.