Edit 2020: I've been using the systemd-answer posted below for several years now, and am quite happy with it.
To facilitate working with remote files, I setup afuse
with sshfs
to run from .bashrc
of local users. It works, after bash init all remote trees from ~/.ssh/config/
are mounted on-demand under ~/scp/
, with correct access rights/restrictions.
This is currently in (a file, that is sourced from) .bashrc
:
exists(){ [[ ${1:0:1} == "/" ]] && { test -f $1 || test -d $1; } \
|| command -v $1 >/dev/null 2>&1; }
# Run an afuse process as daemon
if pgrep -u $USER -f "afuse.*$USER/scp" >/dev/null ; then
echo "afuse/sshfs already running"
else
exists afuse && exists sshfs && exists $HOME/scp \
&& export TAfuseX=$(mktemp --suffix=__afuse) && chmod u+x "$TAfuseX" \
&& echo -e "#!/bin/sh\ngrep '^Host ..' ~/.ssh/co* |colrm 1 5" > $TAfuseX \
&& echo "running afuse/sshfs with nohup" \
&& nohup afuse -o mount_template="sshfs %r:/ %m" \
-o unmount_template="fusermount -u -z %m" \
-o populate_root_command=$TAfuseX \
~/scp 2>&1 > ~/afuse.log
fi
(The above already incorporates a helpful hint from @sato-katsura)
The above makes sure, it normally runs until machine-shutdown. It is independent of X stops/starts. In case of afuse crash (has never been observed by me), the next shell re-spawns it.
Do you see any useful optimizations, pitfalls, or have other hints, even before migrating this to systemd
? How would you wrap that into a systemd user unit?