I had a bit of time to kill yesterday so I thought I'd write a minor script to correct something that was bothering me lately. Then I thought it might be a good opportunity to start up a permanent thread to share useful scripts members may have written.
So, I'll go first.
The following script is to prevent an external USB hard drive from sleeping.
This simple script will keep an external USB platter drive from spinning down and going to sleep. Some external USB drive enclosures do not respect the settings of sdparm, hdparm, or the system itself when it comes to setting a time threshold before an external drive is put to sleep. If a platter drive is put to sleep after a very short idle time it is not only inconvenient, but it can also contribute to premature drive wear. My enclosure was being put to sleep after only three minutes of inactivity, and because of a limitation of the drive's enclosure I could not alter this annoying behavior. This script simply creates a hidden file at a predetermined write interval, to prevent the drive being put to sleep. This may be very important to you if you want a drive to always be accessible for unattended backups etc. The script will keep executing indefinitely in a loop to prevent the drive from sleeping, (unless the user desires to discontinue it).
The same thing can also be accomplished with a cron job or a service. However, I think this method is more flexible, as this method can be started/stopped at any time by various methods. For example, if you'd like this script to start automatically, you can add the script to autostart in the "KDE System Settings". You can also easily start/stop the script anytime via a bash alias or a desktop file. This script also does not require root privileges, so any user can execute or terminate the script.
Follow the steps below to create the script to keep an external USB hard drive from going to sleep.
*Note:
You may need to substitute your username for $USER
in the following commands.
First, create the scripts storage location:
mkdir -p /home/$USER/.local/bin
You can locate the script in an alternate location if you desire, as long as it does not require root privileges to access.
*Note:
You may need to substitute your username for $USER
in the following commands.
With a text editor, create the keep_alive script:
/home/$USER/.local/bin/keep_alive.sh
With the following contents:
#!/bin/bash
# cat /home/$USER/.local/bin/keep_alive.sh
# Script to prevent a USB hard drive from sleeping
time=$(date +%Y-%m-%d@%H:%M)
if [ "$EUID" -eq 0 ]; then
echo "This script cannot be executed as root - exiting script"
sleep 2
exit 1
fi
/usr/bin/touch /run/media/$USER/10TB-WD-2/.stayawake &> /dev/null
if [ -f /run/media/$USER/10TB-WD-2/.stayawake ]; then
echo -e "Drive last awakened @ $time" > /run/media/$USER/10TB-WD-2/.stayawake
fi
sleep 150
exec $0
Save the script, and then make the script executable:
chmod +x /home/$USER/.local/bin/keep_alive.sh
It is advisable, (but not 100% necessary) to give the drive a permanent mount location via /etc/fstab
using the nofail
option. The drive's name and path in the script /run/media/$USER/10TB-WD-2
must obviously be modified to reflect your intended drive's name and path. The time frame to write the file is set via the sleep value in seconds (sleep 150
). If you wish the time interval in seconds to be longer or shorter, then alter the value from 150
. The file's name .stayawake
, is preceded with a dot, so that the file is hidden on your intended drive target. You may alter the name of the file if you desire as well. The hidden .stayawake
file is automatically created by the script, and it also writes a timestamp to the file specifying when it was last written.
If you ever wish to terminate the keep_alive script, run:
killall -9 keep_alive.sh
If you want the script to run automatically, add the script to KDE's autostart at login in System Settings
, (and then restart). You can also use a bash alias to easily start or stop the script from the terminal whenever you choose.
I also wrote two handy desktop files (with customized icons) so the script can be started or stopped from your taskbar/dock/desktop if you'd prefer to use that easy GUI method.
With a text editor, create the hd_awake.desktop
file on your users desktop to start the script:
/home/$USER/Desktop/hd_awake.desktop
With the following contents:
[Desktop Action New]
Exec=/bin/bash -c "/home/$USER/.local/bin/keep_alive.sh"
Name=Prevent Hard Drive Sleep
[Desktop Entry]
Actions=New;
Categories=System;Utility;
Comment[en_CA]=Prevent Hard Drive Sleep
Comment=Prevent Hard Drive Sleep
Exec=/bin/bash -c "/home/htpc/.local/bin/keep_alive.sh"
GenericName[en_CA]=Prevent Hard Drive Sleep
GenericName=Prevent Hard Drive Sleep
Icon=gsmartcontrol
MimeType=
Name[en_CA]=Prevent Hard Drive Sleep
Name=Prevent Hard Drive Sleep
Path=/home/htpc/.local/bin/keep_alive.sh
StartupNotify=true
Terminal=false
TryExec=/home/htpc/.local/bin/keep_alive.sh
NoDisplay=true
Path=
StartupNotify=false
Type=Application
Version=1.0
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
Save the desktop file, and then make it executable:
chmod +x /home/$USER/Desktop/hd_awake.desktop
With a text editor, create the hd_sleep.desktop
file on your users desktop to stop the script:
/home/$USER/Desktop/hd_sleep.desktop
With the following contents:
[Desktop Action New]
Exec=/bin/bash -c "killall -9 keep_alive.sh"
Name=Allow Hard Drive Sleep
[Desktop Entry]
Actions=New;
Categories=System;Utility;
Comment[en_CA]=Allow Hard Drive Sleep
Comment=Allow Hard Drive Sleep
Exec=/bin/bash -c "killall -9 keep_alive.sh"
GenericName[en_CA]=Allow Hard Drive Sleep
GenericName=Allow Hard Drive Sleep
Icon=gnome-disks-state-standby-symbolic
MimeType=
Name[en_CA]=Allow Hard Drive Sleep
Name=Allow Hard Drive Sleep
StartupNotify=true
Terminal=false
NoDisplay=true
Path=
StartupNotify=false
Type=Application
Version=1.0
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
Save the desktop file, and then make it executable.
chmod +x /home/$USER/Desktop/hd_sleep.desktop
After the desktop files are created you can then drag and drop them onto your taskbar/dock or your desktop for easy execution. Below is a screenshot of the "Awake" and "Sleep" buttons (created with the .desktop
files) to the right of the Garuda logo (top left of taskbar).
If the icons I've chosen in the desktop file are not available on your system, then you will need to edit the Icon=
line in the desktop file and choose an alternate icon.
Well that's about it. I hope my simple script helps someone.
Hopefully others will use this thread to share their scripting efforts as well. Feel free to add whatever scripts you've written so that other forum users can benefit and gain scripting knowledge.