Why SpringBoot app cannot access to remote files (sshfs) unless a user is connected to the server?

214 Views Asked by At

This question involves a raspberry pi 4 connected via Wifi to the internet, and running a SpringBoot app that generates a file tree with special values upon user request. The SpringBoot App needs compute the parent path of some files located on the remote server to build the file tree the user has asked.

This all works when the remote data are already mounted when the SpringBoot app is launched from a console.

As I need the SpringBoot app to run automatically and be launched at boot time I changed /etc/rc.local and /etc/fstab like this :

In /etc/rc/local I added su pi -c 'cd /home/pi/MyApp && ./LaunchApp.run &'

and in /etc/fstab I added

[email protected]:               /home/pi/projects/remoteData          fuse.sshfs           port=22,user,identityfile=/home/pi/.ssh/id_rsa,noatime,allow_other

Mind the allow_other so that all user can read and write (not only root), and the path to the ssh key to use.

However the SpringBoot app does start but then it cannot access to the remote data so the file tree cannot be build. If I login via VNC (GUI login), then I am able to click on the mount point which actually mounts and the SpringBoot app can compute the file tree because it can access to the remote data.

But when I close VNC (even without disconnecting from the logged in user) the SpringBoot App cannot access to the remote data anymore.

Running sudo mount -av (or -fav to simulate the mount) in a console led to the mounting of the remote data and the ability for SpingBoot to access to the remote data as expected. But after log out no access to the remote data is possible.

Please note that I tested the fstab command with and without _netdev but it did not change anything regarding the ability for the SpringBoot app to access the remote data.

I also ran ps -A to check which user was running SpringBoot and it showed pi.

Consequently I am wondering why the SpringBoot app can only see the remote data when pi user is connected ?

And furthermore what can I do to make the mount point available to the SpringBoot app without having to manually mount the remote data ?

Please note : I also tried to launch the mounting from rc.local or from ~/.profile (in both cases removing the corresponding line from fstab) by adding this line :

rc.local

su pi -c 'sshfs [email protected]:               /home/pi/projects/remoteData &'

or .profile

sshfs [email protected]:               /home/pi/projects/remoteData

Still SpringBoot was not able to access to the remote data unless I manually log in.

Any help much appreciated

1

There are 1 best solutions below

0
On

I eventually tried to wait some time before mounting the remote data via sshfs, and it worked flawlessly.

So here are the points I followed :

  • removed the sshfs entry from fstab

  • removed the sshfs entry from .profile

  • added a 20 second delay in rc.local :

su pi -c 'sleep 20 && sshfs -o reconnect,ServerAliveCountMax=1,ServerAliveInterval=15 [email protected]: /home/pi/projects/remoteData &'

I think the reason why the sshfs command needed a delay is because the Wifi connection may not be established when rc.local or fstab initially ran. Waiting 20 seconds gives enough time for this connection to be established and the mount to operate.

Please do mind that in my case I also had to add -o reconnect,ServerAliveCountMax=1,ServerAliveInterval=15 option, to prevent the connection to the remote server to be lost after a while (reconnect alone was not enough as described here). What these options do is to ping the remote server every 15 seconds and after 1 failing ping attempts try to reconnect.