Waiting for other pc to come up before mounting samba share

Posted on

Problem :

I am mounting a samba share via /etc/fstab.
My problem is, if they are shutdown and I start them the machine who mounts the share is up first, and therefore the share is not yet available.
Is there a option that he trys to mount it till the share is available?

The problem is, that on these shares are data which applications need to run propperly

Solution :

Don’t mount it automatically via fstab, use a crontab instead:

  1. Set your fstab to not mount the share automatically

    //servername/sharename  /media/windowsshare  cifs  noauto  0  0
    

    You can have various other options there, presumably you already do. The important part is to add the noauto which ensures that

          noauto do not mount when "mount -a"  is  given  (e.g.,  at  boot
                 time)
    
  2. Create a cron job that runs every minute and mounts the share if it isn’t already mounted. Add this line to /etc/crontab

    * * * * *   root    mount | grep windowsshare || mount /media/windowsshare
    

That way, the disk will be mounted as soon as it’s available.


For more fine grained control, you can write a script that i) checks whether the server is online and ii) mounts the share unless it’s mounted. You could then run the script via cron:

#!/usr/bin/env sh
hostname_or_ip_address="1.2.3.4" ## add your WIndows host's name or IP here
if ping -c 1 -W 1 "$hostname_or_ip_address" >/dev/null 2>&1; then
  mount | grep windowsshare || mount /media/windowsshare
fi

Save that script as /usr/bin/check_mount or whatever you please, make it executable with chmod +x /usr/bin/check_mount and then add this line to /etc/crontab

* * * * *   root   /usr/bin/check_mount 

Another option worth looking into is autofs which mounts systems on demand as soon as anyone tries to access them.

Leave a Reply

Your email address will not be published. Required fields are marked *