Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LethalWolfe

macrumors G3
Original poster
Jan 11, 2002
9,370
124
Los Angeles
I have 3 FW drives that go to sleep (no matter what) and I was wondering how to make an Apple Script that could poll the drives every X minutes to keep them awake. I've never worke w/Apple Script before so guidance would be great, and someone actually writing the script for me would be awesome. :D


Lethal
 

Super Macho Man

macrumors 6502a
Jul 24, 2006
505
0
Hollywood, CA
Were you aware of pmset? I think "pmset -a disksleep 0" should do it, maybe.

To actually poll the drives, how about a shell script instead - name it "nosleep.sh", probably has to be run as root. Could also name it .command instead of .sh in order to make it double-clickable but I'm not sure how it would run as root in that case. (I haven't tested it):

Code:
#!/bin/sh
# Writes 3 empty files to 3 different drives and
# immediately deletes them every 10 minutes

for (( i = 1; i > 0; i++ ));
do
  touch tempfile /Volumes/name_of_drive1
  touch tempfile /Volumes/name_of_drive2
  touch tempfile /Volumes/name_of_drive3
  rm -f /Volumes/name_of_drive1/tempfile
  rm -f /Volumes/name_of_drive2/tempfile
  rm -f /Volumes/name_of_drive3/tempfile
  sleep 600
done
 

OutThere

macrumors 603
Dec 19, 2002
5,730
3
NYC
My suggestion would be to use a shell script instead of applescript, and to let cron do the automating, I don't know how much you know about this stuff so I'll keep it simple...

1) Copy this into a plain text file in TextEdit, change the "drive1/2/3" into the names of your drives, and save it as "poll.sh" wherever you want.

Code:
#!/bin/bash

a="/Volumes/drive1/"
b="/Volumes/drive2/"
c="/Volumes/drive3/"

ls $a
ls $b
ls $c

That's basically a script that reads out the names of the files in the root of your drives automatically.

2) Go into the terminal, type:

Code:
sudo cp /the/path/to/poll.sh/ /usr/bin/poll.sh
then
Code:
sudo chmod 777 /usr/bin/poll.sh

that will copy the shell script to a special folder, and set its permissions to be able to be run

3) Again in the terminal, type:

Code:
sudo pico /etc/crontab

then it'll bring up like a really basic text editor, with a list of jobs that your computer will do automatically, so you don't have to run the script by hand. The columns are tab delimited, so keep that in mind. Make the file look like this (or at least add this line if there are others):

Code:
# The periodic and atrun jobs have moved to launchd jobs
# See /System/Library/LaunchDaemons
#
# minute        hour    mday    month   wday    who     command                        
*/8                                           root    sh /usr/bin/poll.sh

That will tell your computer that every 8 minutes it should run the script and get a list of the files on your drives.

Do all that and your problem should be solved.
 

Poeben

macrumors 6502
Jul 29, 2004
346
0
I've been messing with this for a few days. Here's what I can add to the discussion....

Tiger no longer uses 'cron' to schedule things. You should be using 'launchd' combined with .plist files. I also chose to use 'touch' instead of 'ls' as it should use less resources, at least if there are many files on the drive. I also made my touched file invisible to the finder by prefixing the filename with a dot.

So my script is based on the following command:

touch -m /path/to/.file.txt

and the .plist (saved in ~/Library/LaunchAgents/) is something along the lines of:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.something.nieSchlaf</string>
	<key>Program</key>
	<string>/usr/local/nieSchlaf.sh</string>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/local/nieSchlaf.sh</string>
	</array>
	<key>ServiceDescription</key>
	<string>Will attempt to keep external drives from spinning down by utilizing the 'touch' command</string>
	<key>StartInterval</key>
	<integer>600</integer>
</dict>
</plist>

You will have to make the script executible

chmod +x script.sh

You will also likely have to create the ~/Library/LaunchAgents/ directory.

You set everything in motion using:

launchctl load ~/Library/LaunchAgents/<your_plist_file.plist>

I found this site helpful.
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
Code:
sudo chmod 777 /usr/bin/poll.sh

that will copy the shell script to a special folder, and set its permissions to be able to be run

Bad Idea. Very Very BAD IDEA. That is way too insecure. Anyone will be able to modify that script into something malicious. You'd get your box owned in no time flat.

I suggest 700 and not running it as root
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
the .plist (saved in ~/Library/LaunchAgents/)
for clarification: scripts launched from that location will run iff the relevant user is logged in. for the task at hand, that's probably fine.

for those who wish to run a script without regard to who is logged in or not, the .plist file should be placed in:
/Library/LaunchDaemons/

obviously, one has to be logged into root both to do the copy and run the launchctl load command.

also: run launchctl list to verify your load command worked. run launchctl unload <name of .plist> to unload it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.