PDA

View Full Version : Script to poll FW drives every 10min?




LethalWolfe
Jul 1, 2007, 12:29 AM
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
Jul 1, 2007, 01:08 AM
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):


#!/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
Jul 1, 2007, 03:03 AM
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.


#!/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:


sudo cp /the/path/to/poll.sh/ /usr/bin/poll.sh

then

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:


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):


# 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.

LethalWolfe
Jul 1, 2007, 04:12 PM
Cool, thanks guys. I'll try these out in the next day or so.


-Lethal

Poeben
Jul 3, 2007, 02:35 PM
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:

<?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 (http://www.macgeekery.com/tips/all_about_launchd_items_and_how_to_make_one_yourself) helpful.

SC68Cal
Jul 3, 2007, 03:08 PM
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
Jul 3, 2007, 07:21 PM
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.