Ubuntu: automatically unmount sshfs network drives before VPN disconnects on suspend


Reasonably new linux user here. For my work, I regularly mount two network drives via sshfs in Xubuntu 15.05. Now I've written a little bash script to mount/unmount both drives & create a symlink to them in local bin to make things easier for myself, and they work fine. The problem is that I can only mount these network drives when VPN is connected, and when I suspend my laptop (which I sometimes have to do very unexpectedly) the VPN connection gets broken without the disks being unmounted. After resuming the session later, the disks seem to be stuck between mount and unmount. I cannot access the disks, but I can also not unmount them, or even remount them. Does anyone know a way to make sure that any time the VPN connection is broken for some reason (either on suspend, or because the network fails or whatever reason) both disks are automatically unmounted just before to prevent this problem?

Thanks so much! Narameh


Try this

Make script witch will umount sshfs drives when vpn is terminated.

Make script in the folder /etc/network/if-down.d/ with execute permissions 755

# Check for specific interface if desired  [ "$IFACE" != "ppp0" ] || exit 0  # Do something    umount /path/drive  

When int ppp0 will go down command umount /path/drive will be executed

Also you can make "auto mount" if int ppp0 go up

Make script in /etc/network/if-up.d/

# Check for specific interface if desired  [ "$IFACE" != "ppp0" ] || exit 0  # Do something    your_script_for_mounting_drives  

Solutin No 2

Make script link_stat

nano /path/link_stat

#!/bin/bash    if ip addr show ppp0 | grep 'state UP'; then      echo "Network ok."  else      echo "$ERR Network down, umounting"      #put_here_your_sommand_for_umount  fi 2>&1 >> /path_to_log/stat_log  


chmod 777 /path/link_stat  

Run script with cron

sudo crontab -e

run command on every 30 sec

* * * * * /path/link_stat  * * * * * sleep 30 && /path/link_stat  

This will check for vpn status and if status if dovn will be umount drives

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »