Mounting Samba share whenever it's available, unmounting when it's not

Mounting Samba shares with CIFS:

Edit your /etc/fstab file to automatically mount your Samba shares:

//<server>/<share> <mount point> cifs rw,_netdev,user=<username>,password=<password>,uid=<uid>,gid=<gid> 0 0

Notice the _netdev option:

CIFS doesn't know about this option and will throw a warning about it, but you should leave it in.

This option ensures that the drives won't get mounted if you have no network or if you are on the wrong network.

It also makes sure your shares get mounted/unmounted when you switch networks. That's because Debian's if* scripts are triggered when your network configuration changes, and they watch all the filesystems with the _netdev option and do the right thing automagically.

Source

you can check man mount for more information about _netdev

To install the CIFS tools

sudo apt-get install cifs-utils

Write the scripts to mount and to unmount the share.

Place them in "/etc/network/if-up.d" and in "/etc/network/if-down.d" respectively. These are the locations for scripts that are run when the network connection is established or closed.

However, if you are looking for a particular server to come up or down online, the only solution I see is to make a cron job that checks the server connection periodically, keeps the state and runs the scripts accordingly.