[SOLVED] Faster ways to test if a network share is available or not without waiting 15 seconds
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Faster ways to test if a network share is available or not without waiting 15 seconds
Got a network share mounted on directory /mnt/share. If the server where it is located is switched off and a script tries to access a certain file in the share, it fails of course but it fails after 15 seconds. What are some faster ways to test if the share is up and running before trying to access the file, and do so repetitively in a script every second?
Distro: peppermintOS on all systems, based on ubuntu.
Just found smbtree -b still shows the same output after the server is disconnected, even minutes later. Ping looks more promising as you control the time it waits. Any test for the existence of the share?
What are some faster ways to test if the share is up and running before trying to access the file, and do so repetitively in a script every second?
Example, check for machine, if up do something, else not.
Code:
#Check for machine up every 5 seconds
while :; do
ping -c1 172.16.0.13 &> /dev/null
if [ $? -eq 0 ]; then
echo "Machine is up"
else echo "Machine is down"
fi
sleep 5
done
You never posted what type of share or how it is mounted i.e. via your script or fstab etc. From post #3 we assume it is CIFS. Since you are using peppermint why not nfs or maybe sshfs or maybe use autofs or systemd automount.
Depends on the distribution but you can test to see if the share is available using smblient or maybe avahi-browse. Typically one might assume that if the server is powered on that the samba will automatically start?
Using CIFS only because it's preconfigured in the distro. I mount it with fstab, and if the server is down and "ls /mnt/shared" fails, and then the server is powered on, then "ls /mnt/shared" works. Might the other options report unavailability of the share faster?
Anyway, ping as in teckk's code is probably less wasteful of bandwidth. And it can have -W 2 added to give up waiting for a response after 2 seconds when the server is down. Tried avahi-browse -a and it does not exit, is this normal? Turning off the server, and running avahi-browse -a again reports exactly the same lines.
Maybe the process that changes the file could send a UDP packet that a process on the other side is listening for? Is there a command that listens on a port and executes a script when a packet arrives?
EDIT: There is indeed, with short messages instead of UDP packets:
socat -u tcp-l:7777,fork system:./getmsg.sh
where getmsg.sh is the script to execute when a message arrives on port 7777:
#!/bin/sh
read MESSAGE
echo "$MESSAGE"
And now the process (actually a script of mine) that changes the file can notify the other side like this:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.