How to detect if mount point exists from init.d script?
Solution 1:
init
scripts are started in order as defined by the S## numbers. Newer versions of Unix (at least on Linux) start the same ## numbers in parallel (Although you can turn that feature off...) All you have to do it use a ## that's after the network and fsmount
number. Then it should work. However, if the fsmount
starts in the background, the easiest is probably to probe for files on the mounted drive. Something like this:
while ! test -f /mnt/over-there/this/file/here
do
sleep 1
done
This will wait until the file appears. If not there yet, sleep for a second then try again.
To avoid the potential problem of someone creating the file you are testing on the local computer, you may instead want to use the mountpoint
command line as in:
while ! mountpoint -q /mnt/over-there
do
sleep 1
done
(From comment below.) The -q
is to make the command quiet.
--- Update: timeout after 30 attempts
In shell script you can also count and test numbers:
count=0
while ! test -f /mnt/over-there/this/file/here
do
sleep 1
count=`expr $count + 1`
if test $count -eq 30
then
echo "timed out!"
exit 1
fi
done
This will stop if count reaches 30 (30 seconds of sleep plus the time it takes to check whether the file is available or not) after which it prints the error message "timed out!".
--- Update: in case you were to switch to systemd
With systemd, the Unit section supports:
ConditionPathIsMountPoint=/mnt/over-there
which does the same thing as the above script, without the timeout. This statement prevents starting your command until the mount exists.
Solution 2:
Because the destination directory can be present but not mounted, I prefer simonpw test on /proc/mounts
if ! grep -qs '/the/mounted/dir' /proc/mounts; then