Chris wrote:
Hello,

I want to run a script to rsync local files to a NAS mounted to /mnt/music. Sometimes the NAS is not running, and I want to prevent the script from writing to the mount directory: is there any easy way to prevent this?

Thanks,

C

if [ -f /mnt/music/somefile ]
then
  # run your script here.
fi

There are other ways to do the test, depending on how the mount was done: at boot with fstab, mounted by a user using 'mount', automounted, or by some other method.

If mounted at boot time but the mount fails, output of 'mount' with no args would not have /mnt/music in it, so grep could be used in that case.

If mounted by the user, then mount would fail if the NAS were down, and that could be tested for (assuming the 'mount' is part of the script).

Or, 'df /mnt/music', which returns info for the mounted device, if mounted, and for the root device if not.

These latter suggestions require pipes to grep for values that are valid for the mounted condition, with checks on the grep exit status determining whether or not you continue.

For example, with the 'df' suggestion:

if df /mnt/music | grep /mnt/music > /dev/null
then
  # run your script here.
fi

Using I/O redirection, with grep, is more portable, but for Linux systems (or, specifically, any system with Gnu grep), you can use 'grep -q /mnt/music' instead.

--
Bob McGowan

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to