iTranslated by AI
A Script to Prevent Specific External HDDs from Sleeping on macOS
For some reason, I have become unable to properly configure external HDD sleep control on macOS. I haven't fully investigated whether it's an issue with my environment or a change in recent OS specifications. I tried techniques such as manipulating the disksleep value with the pmset command, but they didn't work. I suspect both the characteristics of the LaCie HDD device I am using and changes in the OS specifications.
Giving up on investigating this problem any further, I thought of another way to prevent sleep. I figured, "Wouldn't it work if a program periodically accessed the HDD I don't want to go to sleep?" and decided to register a script that writes an empty file at regular intervals to a LaunchAgent.
LaunchAgent is a mechanism for processes managed and executed by a service called launchd. It is used to execute tasks at startup (login) on macOS or to run programs periodically, similar to cron or a daemon.
Script for Writing Empty Files to Arbitrary Volumes
I created a shell script named wakeupdrives.sh as follows. Although I referred to "HDD" at the beginning of the article, the script does not distinguish between HDD and SSD.
#!/bin/sh
## Names of volumes to target for sleep suppression (multiple names can be listed)
volumeNames=("Alpha" "Beta")
## Name of the empty file
wakeupFileName=".wakeup"
## File for toggling the script on/off
enablerFilePath="$HOME/WakeupDrives"
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
## Exit the script if `~/WakeupDrives` is not found
if [ ! -e $enablerFilePath ]; then
echo "$timestamp: [STOP] exit wakeupdrives.sh because “~/WakeupDrives” file is not exist." # debug line
exit 0
fi
for volumeName in ${volumeNames[@]}; do
volumePath="/Volumes/$volumeName"
if [ -e $volumePath ]; then
#echo "$volumePath/$wakeupFileName" # debug line
## Write an empty file to "/Volumes/VOLUMENAME/.wakeup"
echo "" > $volumePath/$wakeupFileName
echo "$timestamp: [OK] $volumePath/$wakeupFileName" # debug line
fi
done
volumeNames
List the names of the volumes you want to target for sleep suppression. Use the names as they appear in Finder. For example, a label like "Macintosh HD".
wakeupFileName
Set the name of the empty file to be written to suppress sleep for the target volume.
enablerFilePath
This script will operate if an empty file named $HOME/WakeupDrives exists. You can stop the script by moving or deleting this file. Since there is no explicit GUI, the presence of the file acts as a power switch.
LaunchAgent plist
This is a plist for registering with LaunchAgent. By registering this, you can periodically execute the target command in the background on macOS. Place the plist file under ~/Library/LaunchAgents.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>wakeupdrives</string>
<key>Program</key>
<string>/usr/local/bin/wakeupdrives.sh</string>
<key>ProgramArguments</key>
<array/>
<key>StartInterval</key>
<integer>25</integer>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/Users/YOURHOME/agent_wakeupdrives_err.log</string>
<key>StandardOutPath</key>
<string>/Users/YOURHOME/agent_wakeupdrives_stdout.log</string>
</dict>
</plist>
Key points:
-
Program
- Path of the command to execute
-
StartInterval
- Periodic execution interval (seconds)
-
RunAtLoad
- Whether to execute at desktop startup (at login)
-
StandardErrorPath
- Output path for stderr (change
YOURHOMEabove to your home folder name)
- Output path for stderr (change
-
StandardOutPath
- Output path for stdout (change
YOURHOMEabove to your home folder name)
- Output path for stdout (change
Detailed parameter documentation:
Registering the LaunchAgent
Register the plist using launchctl. If successful, it will be registered in the macOS Login Items settings.
launchctl load ~/Library/LaunchAgents/wakeupdrives.plist
Troubleshooting
Unload and then reload
launchctl unload ~/Library/LaunchAgents/wakeupdrives.plist
launchctl load ~/Library/LaunchAgents/wakeupdrives.plist
Check plist syntax
plutil -lint ~/Library/LaunchAgents/wakeupdrives.plist
Grant execution permission to the script file
chmod 664 /usr/local/bin/wakeupdrives.sh
Grant Full Disk Access to /bin/sh
If "operation not permitted" appears in the error file, sh might be unable to access the external disk.
Try granting Full Disk Access to /bin/sh in "Privacy & Security" within the System Settings.
Check if the com.apple.quarantine attribute (@) is attached to the script file
This attribute is attached to downloaded files, so try running the following command to remove it.
xattr -cr /PATH/TO/wakeupdrives.sh
Confirm that "@" is not present using ls -l:
ls -l /PATH/TO/wakeupdrives.sh
Verify by printing to StandardOutPath
If the script file is being executed but not working correctly, try verifying it by outputting to StandardOutPath.
When the LaunchAgent detects an error, the details should be output to StandardErrorPath.
Discussion