Script to poll FW drives every 10min?

Discussion in 'Mac Programming' started by LethalWolfe, Jun 30, 2007.

  1. LethalWolfe macrumors G3


    Jan 11, 2002
    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

  2. Super Macho Man macrumors 6502a

    Super Macho Man

    Jul 24, 2006
    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 "", 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):

    # Writes 3 empty files to 3 different drives and
    # immediately deletes them every 10 minutes
    for (( i = 1; i > 0; i++ ));
      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
  3. OutThere macrumors 603


    Dec 19, 2002
    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 "" wherever you want.

    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/ /usr/bin/
    sudo chmod 777 /usr/bin/
    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/
    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.
  4. LethalWolfe thread starter macrumors G3


    Jan 11, 2002
    Los Angeles
    Cool, thanks guys. I'll try these out in the next day or so.

  5. Poeben macrumors 6502

    Jul 29, 2004
    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" "">
    <plist version="1.0">
    	<string>Will attempt to keep external drives from spinning down by utilizing the 'touch' command</string>
    You will have to make the script executible

    chmod +x

    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.
  6. SC68Cal macrumors 68000

    Feb 23, 2006
    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
  7. zimv20 macrumors 601


    Jul 18, 2002
    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:

    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.

Share This Page