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:


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

# 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\#"

# 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

The .plist file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

The copy command

sudo cp /Scripts/ /Library/LaunchDaemons/

Now don’t forget to load the .plist file

sudo launchctl load /library/LaunchDaemons/

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

Thanks to these useful sources: