Automatically Wake Server and Mount Shares in Mac OS X


Recommended Posts

I setup my server to automatically sleep when inactive and I wanted it to automatically wake up when I accessed one of the shares.  I was able to get it working using AutoFS and an executable map file.

 

Hopefully other Mac users will find this useful.

 

Contents of the executable map file are pasted below and attached in a zip

 

 

#!/usr/bin/perl

#--------------------------------------------------------------#

# AutoFS executable map to wake server when accessing a share. #

#                                                              #

# When a share is accessed we check to see if the server is    #

# awake and if not then a magic packet is sent to wake it up.  #

# The share is mounted when a successful ping response is      #

# received from the server.                                    #

#                                                              #

# Quick Setup:                                                #

#                                                              #

# 1. Set the values below to match your unRAID setup for      #

#    $host, $hwaddr, @shares, $user and $passwd                #

#                                                              #

# 2. Copy to /etc and set execute bit                          #

#    sudo cp auto_unraid /etc                                  #

#    sudo chmod +x /etc/auto_unraid                            #

#                                                              #

# 3. Add the following line to /etc/auto_master                #

#    /Volumes/unraid      auto_unraid                        #

#                                                              #

# 4. Load your changes                                        #

#    sudo automount -vc                                        #

#                                                              #

#--------------------------------------------------------------#

use strict;

 

# hostname or IP of unRAID server

my $host  = '192.168.1.100';

 

# MAC Address for unRAID server

my $hwaddr = 'XX:XX:XX:XX:XX:XX';

 

# array of AFP share names

my @shares = ("Documents","Development","Movies","TimeMachine");

 

# username and password for mounting shares

# set to empty string if not needed

my $user  = '';

my $passwd = '';

 

 

# =============================================================

# You should not need to modify anything below this point

# =============================================================

 

if (@ARGV == 0) {

    print join("\n", @shares)."\n";

    exit;

}

 

my $is_asleep = "ping -c1 -t1 ".$host." >/dev/null 2>&1";

system($is_asleep);

if ($? != 0) {

    use Socket;

    my $port        = getservbyname('discard', 'udp');

    my $hostaddr    = gethostbyname('255.255.255.255');

    my $sockaddr_in = pack_sockaddr_in($port, $hostaddr);

    my $proto      = getprotobyname('udp');

    my $pkt        = chr(0xFF) x 6 . join("", map {chr(hex($_))} split(/:/,$hwaddr)) x 16;

    my $count      = 0;

    socket(S, AF_INET, SOCK_DGRAM, $proto);

    setsockopt(S, SOL_SOCKET, SO_BROADCAST, 1);

    send(S, $pkt, 0, $sockaddr_in);

    do {

        $count++;

        system($is_asleep);

    } while ($? != 0 && $count < 15);

}

 

if ($user && $passwd) {

    print "-fstype=afp afp://".$user.":".$passwd."@".$host."/".$ARGV[0]."\n";

} else {

    print "-fstype=afp afp://".$host."/".$ARGV[0]."\n";

}

 

exit;

 

auto_unraid.zip

Link to comment
  • 4 weeks later...
  • 3 months later...

This is exactly what I've been looking for too, thanks.

 

I've ran into a little difficulty though... I think I have it all set up properly but it doesn't wake my server.

 

I think it may be to do with the way I've mounted my shares on osx, I have just done it through go > connect to server. Do they have to be mounted so osx 'remembers' them somehow?

 

My end goal is to use the shares in xbmc. I have a mac mini attached to my tv but I am not experienced in osx. Any help appreciated!

Link to comment

This is exactly what I've been looking for too, thanks.

 

I've ran into a little difficulty though... I think I have it all set up properly but it doesn't wake my server.

 

I think it may be to do with the way I've mounted my shares on osx, I have just done it through go > connect to server. Do they have to be mounted so osx 'remembers' them somehow?

 

My end goal is to use the shares in xbmc. I have a mac mini attached to my tv but I am not experienced in osx. Any help appreciated!

 

This will only wake the server if the share are accessed at the mount point you setup in your /etc/auto_master.  If you used /Volumes/unraid like the example in the script, then the server will only wake when you access /Volumes/unraid/Documents for example.

 

Instead of using "Go > Connect to Server"  use "Go > Go to Folder" and enter "/Volumes/unraid" or whatever you setup in your /etc/auto_master.  You should see a list of the shares you defined and clicking one of them will wake the server.

 

If you setup xbmc to point to the server through the mount point you setup it will wake the server when the share is accessed, just use the /Volumes/unraid/sharename paths.

 

Greg

Link to comment
  • 4 weeks later...

Hello.

I'm glad someone made this script :-) As a complete n00b, I'm wondering if I run it on my mac or on the server. Obviously, it should be on the Mac, right?

 

I really want to use it because I'm storing all my music on the server and when I got micro-cut or complete disconnection, iTunes keeps reverting back the folder location of my library locally, thus all the music is "not found", I have to manually reset the location and do all the presets once again.

 

Envoyé depuis mon HTC Sensation XE with Beats Audio Z715e avec Tapatalk

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.