how to mount/umount folder with bindfs/fusermount from root account without privileges?

This is my bash script into init.d:

#!/bin/bash
case "$1" in
  'start')
    # mount
    bindfs -n /home/my_user/.local/share/Cryptomator/mnt /home/my_user/drivefolder
 ;;
  'stop')
    fusermount -u /home/my_user/drivefolder
 ;;
  *)
    echo "Usage: $0 { start | stop }"
 ;;
esac

so i run it from root account:

/etc/init.d/drivemount.sh start
/etc/init.d/drivemount.sh stop

What I want is to mount/umount /home/my_user/drivefolder from root account to my_user account without privileges (so that the mounted folder can be manipulated without privileges).

is there any way to do this? (without having to run the script in my_user account without privileges and from another location)

Clarification: The script does not run automatically. What I want is to run the script manually, either from root or from my_user with sudo only from /etc/init.d location (for reasons unrelated to the question)

PD: I have read that with eval it can be done but I don't know.


Solution 1:

Instead of bindfs … use sudo -u my_user bindfs ….

If root runs the script then sudo should allow becoming my_user without password. If my_user runs the script then sudo should allow becoming the same user without password. If any user runs the script with sudo then it will be as if root run the script.

(Maybe some settings in sudoers can change this behavior regarding passwords. I don't know for sure. I expect the common sane configuration to behave as described though.)

My tests indicate root doesn't need sudo -u my_user before fusermount -u. However if another user simply runs the script (without sudo) and they are able to authenticate as my_user then they will be able to mount. For consistency it will be good to allow them to unmount in the same way, so sudo -u my_user before fusermount … may be a good thing.