Prevent Raspberry PI NAS Drives from Shutdown

There are a lot of tutorials showing you how to use hdparm to prevent your NAS drives on the Raspberry Pi spinning down. But if you are one on those unfortunate users like me that has a dock/drive enclosure that spins down on its own, regardles of what you set with hdparm, this is a solution that works. A short script to store the Date and a random number to a file ist good enough. The good thing is if you cat <file> the output you can exactly see when crontab started it (see down below). That's all. No more spin down which is good for your NAS drives, and no spin-up, which takes some time and is very annoying if your filemanager is stuck until the drive is ready. Please note: This is only recommended for NAS drives. They are meant to work 24/7. If you use a common harddisk, it might wear out faster. the LOAD CYCLE COUNT is what you're looking for. If it's high you may even use this on normal HDDs. Whatever you do, please consider it carefully and do it at your own risk.

The short script (I used the name nospindown):

#!/bin/bash
#
# Prevent sleep by writing to drive (set interval in crontab)
#
RANDOM=$$
FILENAME=/media/nas1/nospindown.rnd
echo
date >>$FILENAME
echo $RANDOM >>$FILENAME 
echo "---------------------" >>$FILENAME

Change /media/nas1/ according to the path of your NAS-drive.

Add this to your crontab with crontab -e */9 * * * * /home/pi/bin/nospindown

Change /home/pi/bin/nospindown according to the name of the script with full path. For my dock I need to set it to 9 minutes. After 10 minutes it shuts down my drive. to prevent this you should safely use 1 minute before time runs out. Otherwise it could happen that the drive spins down and up again immediately which would be even worse.

Don't forget to chmod 744 /home/pi/bin/nospindown

Have a lot of fun.