2

There are a bunch of files in a remote directory which I want to access over sshfs and then script some operations. In practice, I wish to access different files at different times, and perform different operations on those files.

Option 1: Mount the whole directory (one script). Then, run any file-specific script of my choosing.

Option 2: Bundle the process of mounting the directory together with the file-specific script.

Disadvantage of option 1: Before running the file-specific script, I forget whether the folder is mounted at all. It takes only 10 seconds to check, but is a minor irritation. I don't want the folder to be mounted at all times, or by default.

Disadvantage of option 2: When processing a second or third file, I end up using sshfs to mount a remote directory which is already mounted. Can this do any harm?

With respect to option 2, I suppose it's possible to check whether the directory is already mounted, like this: Check if directory mounted with bash [closed]. Does that sound like the best solution?

2

Talking about "option 2", it's not a good idea to mount an already mounted remote directory. If you mount it on another mount point and depending on what is your processing, you'd lose the lock mechanism. Furthermore, by default, fuse won't make any mount if the mount point is not empty.

IMHO, the best way to proceed is what you say: check if the remote directory is already mounted, and if not, then mount it. It's only a few lines to append in your script, so easy to manage. For example:

REMOTE="user@remote:/some/dir"

MOUNTPOINT=$( mount | grep -E "^${REMOTE}/? " | awk '{print $3}' )
if [ -z "$MOUNTPOINT" ] ; then
    echo "Mounting remote directory..."
else
    echo "$REMOTE is already mounted on $MOUNTPOINT"
fi
  • Thanks, that makes sense - I suspected that mounting twice was asking for trouble. When I asked the question, I was thinking about multiple mounts on a single client. But it also occurs to me to ask, with sshfs, is there any way client A can ask server 1 whether a directory was already mounted by some other client? – SauceCode Mar 26 '15 at 13:44
  • Yes, 2 different clients can simultaneously mount a same directory from the same remote server. In this case, client A cannot know if the directory is also mounted by client B. So if client A is editing a file that client B is also editing, it will result in lost of data (unless your editor manage lock files, as vim or emacs) – apaul Mar 26 '15 at 13:51
  • Got it. Good to know. – SauceCode Mar 26 '15 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.