3

I use sshfs to do useful things on my dev box. I love it. But, I usually forget to fusermount -u before I close my lid (triggering hibernate). Then, when I come back, sshfs is frozen and I have to clean up.

I'd much rather have a script in /etc/pm/sleep.d that iterates over all my sshfs mountpoints and DWIM. I've got the fusermount -u code written with df -l checks. But, in testing, I've determined that I also need to cd out of all the mountpoints in order for fusermount -u to work.

But, I cannot figure out how to iterate over my open bash terminals and do useful things in them. Help?

Rob Kinyon
  • 141
  • 3
  • 1
    What about unmounting lazily (`fusermount -u -z`)? – Petr Skocik Jun 18 '15 at 21:16
  • [You can remotely force a process to change its working directory.](http://stackoverflow.com/questions/2375003/how-do-i-set-the-working-directory-of-the-parent-process/2375174#2375174) But as the author of that post says, DON'T DO IT! – Celada Jun 19 '15 at 02:31

1 Answers1

2

You can possibly do this by registering a custom user signal handler in ~/.bashrc:

trap '[ "$PWD" = "/my/sshfs/mount" ] && cd ~' SIGUSR1

Then this handler will get invoked whenever you send a SIGUSR1 signal to bash processes:

killall -u "$USER" -USR1 bash

This comes with a few caveats:

  • The signal handler must be registered for all (interactive and non-interactive) bash instances, otherwise the default action (quit) will be taken. I think using ~/.bashrc will generally do this. However, there are options to bash that will override the sourcing of this startup file.
  • Any of the shell processes may be in the middle of something that relies on being in the given sshfs directory. Randomly changing out of this directory could cause arbitrary undefined behaviour, depending on what the given script is doing at the time (or later).
  • There may well be other processes started from a bash process that are still in the given directory. There is not a whole lot that can be done for these processes, other killing them.
Digital Trauma
  • 8,404
  • 2
  • 23
  • 39
  • Setting the trap in `.bashrc` only makes it take effect in **interactive** shells. There's no reliable way to make it take effect in non-interactive shells (even if you managed to do so the script could have its own trap for USR1). – Gilles 'SO- stop being evil' Jun 18 '15 at 21:51
  • @Gilles I was wondering as much. I don't really like this solution, but threw it out there in the hope it might get someone thinking (of something better) – Digital Trauma Jun 18 '15 at 22:11
  • That's basically what I was looking for. I'm going to have a very limited set of activities inside that mountpoint. But, outside of a regex, is there any way to determine if I'm in that directory tree (that or some child)? – Rob Kinyon Jun 18 '15 at 23:26
  • @RobKinyon `df -T .` might be useful. It shows a) the mountpoint for the current directory and b) the filesystem type. Perhaps this will show up as `sshfs` or something similar? – Digital Trauma Jun 18 '15 at 23:34