Mac OS X launchd Automount Network Shares

As Apple seem to like to dictate what they should do with the world, they kindly got rid of the robust amazing cron feature of linux, oh and they also seem to of got rid of /etc/fstab … nice so basically you now have to make shell scripts and load them with launchd to do anything automated.

Thanks apple for making life harder as ALWAYS.

Well as this is quite mind boggling upon first look it needs to be written down as my brain just can’t fit all in.

Basically you need to do three things.

  • Create a shell script containing your commands you want to run.
  • Create .plist file with a load of jazz in it which can seem pretty confusing but not to apple, but more specifically contains the path to your shell script and the frequency you want it to run.
  • Copy the .plist file to /Library/LaunchDaemons/
  • Load the .plist file via launchctl load

The shell script:

[shell]
#!/bin/bash

local_admin_user="xxx" # an admin user name on the computer running this script
local_admin_pswd="xxx" # the password for the above user
mount_point="/Volumes/Myshare" # the name the mounted share point will be known as on this computer

host_name="my-host.local" # the domain name or IP of the system hosting the share point
share_point="MyShare" # the name of the remote share point
share_owner="userX" # the user name of the owner of the remote share point
share_pswd="xxx" # the password for the above user

# IMPORTANT:
# If the share_pswd contains any symbol characters, they need to be escaped.
# So, instead of: share_pswd="abc&123#"
# Do this: share_pswd="abc\&123\#"

#IMPORTANT
# Because I am running this at startup, the mount kept failing because the network wasn’t ready yet
# Adding this delay fixes that issue, only took 3 hours of debugging…. doh
sleep 15

# let’s clean up just in case the mount was dropped
echo $local_admin_pswd | sudo -S -u $local_admin_user umount $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user rmdir $mount_point

# basic steps to mount the private share point
echo $local_admin_pswd | sudo -S -u $local_admin_user mkdir $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user chown $local_admin_user $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user chmod +rwx $mount_point
echo $local_admin_pswd | sudo -S -u $local_admin_user mount_afp afp://$share_owner:[email protected]$host_name/$share_point/ $mount_point
[/shell]

The .plist file

[html]
<?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>com.apple.automount.readynas</string>
<key>ProgramArguments</key>
<array>
<string>/Scripts/automount_readynas.sh</string>
</array>
<key>UserName</key>
<string>root</string>
<key>UserGroup</key>
<string>wheel</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
[/html]

The copy command

[shell]
sudo cp /Scripts/com.apple.automount.readynas.plist /Library/LaunchDaemons/
[/shell]

Now don’t forget to load the .plist file

[shell]
sudo launchctl load /library/LaunchDaemons/com.apple.automount.readynas.plist
[/shell]

So with all that done, restart your mac and see if it works!

Thanks to these useful sources:

http://answer123.com/misc/launchd_notes.html
http://www.gregwillits.ws/articles/using_launchd_to_mount_afp_share_point_upon_startup