Saturday, May 8, 2010

Mount remote filesystem with sshfs in crontab

So, I use my remote fileserver for XBMC by mounting it via sshfs (live-stream 1080 video FTW!).

Sometimes the server has to be rebooted though, and I want the mount to be automatically re-mounted when the system is back up again, and I use crontab for that. One problem still exist, and that is that the key I use is passphrase protected, something that is good, and since I use ssh-agent it's not really a problem:

*/5 * * * * test -d /mounts/remote.sys/video || . /home/myuser/.ssh/environment ; /usr/bin/sshfs remote.sys:/data /mounts/remote.sys/ -o Cipher=blowfish -o transform_symlinks -o follow_symlinks -o allow_other

So, what does it do? test -d will test if "/mounts/remote.sys/video" is a directory (the -d). If not, "||" means "not equal", so if it isn't a directory, then do the command following "||". Here I first source the environment variables from ssh-agent, then I just run sshfs to mount the remote system.


Very nice indeed!

2 comments:

  1. Hello there. I'm also trying to mount a drive with sshfs using crontab, but it fails all the time. The same command works perfectly if i write it myself in a terminal, but not when used in crontab with @reboot.

    I see that you "source the environment variables from ssh-agent", but i seem to be missing this file. Is it something you have created yourself?

    ReplyDelete
  2. Yeah. As soon as I login I want to see if ssh-agent is running, so I start that with a some functions in my .profile:

    http://cacb7500caa60995.paste.se/

    This means you will only need to enter your password when your system is restarted.

    ReplyDelete