Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
I am working on a robotics project and I have a small server running onboard. I am writing a small script to act as a failsafe. If the internet connection goes down, then this script will send an ascii x over the serial connection which will halt all the motors and everything. So here's what I have so far:
Code:
#!/bin/bash
while [1];
do
echo Link Up
ping -c 3 google.com
if [ $? -ne 0 ]; then
echo Link Down!!
#here's where the serial writing would take place
fi
done
Right now this errors (it says that it is expecting a 'do' instead of the 'done' on the last line). I have also been trying to figure out how to send stuff over serial on the command line. I would really appreciate some help or a link to a shell script with some similar stuff in it.
I have indeed. I can't test the connection right now, but would something like this work?
( stty -F
echo "x"
) < /dev/ttyUSB0 > /dev/ttyUSB0
This was an example I found (http://en.wikibooks.org/wiki/Serial_...g/Serial_Linux). I have a question about this though. First, will this keep the connection open continuously? I need the connection to only be open for the time it takes to send the character over.
Re your first script: The while true;do ... done loop will eat all your CPU cycles if run as a simple script. I'd suggest either a sleep n inside the loop so the ping is not running continually or an appropriate nice setting for the process.
P.S.: Why ping three times? Wouldn't one suffice? And, is pinging google really the appropriate method? Consider using something like this:
Code:
#!/bin/bash
# Name of wireless connection
link=wlan0
# Sanity check
grep $link /proc/net/wireless 2>&1 1>/dev/null
[ $? -ne 0 ] && echo $link is not a defined wireless connection name. && exit 255
# Check loop
while true
do
if [ $(($(grep "$link" /proc/net/wireless | cut -f 3 -d " ")+0)) -eq 0 ]
then
echo -n $link Up\r
sleep 10
else
echo $link Down!!
# Here's where the serial writing would take place
fi
done
Note that this assumes you're using a wireless connection. Look in /proc/net for your system specifics.
Thanks PTrenholme, I figured there was a better way to do it, but I only know how to write pretty basic shell scripts so this is a good learning opportunity for me. If you have time, would you mind quickly going over how that script works?
I couldn't get PTrenholme's script working correctly. When I disconnected from my access point, it still said that the link was up. There weren't any errors, am I doing something wrong?
I have indeed. I can't test the connection right now, but would something like this work?
( stty -F
echo "x"
) < /dev/ttyUSB0 > /dev/ttyUSB0
[snip]
First, will this keep the connection open continuously? I need the connection to only be open for the time it takes to send the character over.
It will open /dev/ttyUSB0 for output as long as it takes to run the commands between parentheses. I don't know if /dev/ttyUSB0 is the correct "device file" for the serial port on your system.
The code is more elaborate than needs be. Is it a cut down version of code that also read from the serial port? This is a more minimal way of doing the same thing
Code:
echo 'x' > /dev/ttyUSB0
Regards your earlier post about wanting to do this "If the internet connection goes down" can you be more precise? I'm ignorant about wireless connections but suspect PTrenholme's suggestion tests only the link from your computer to the wireless LAN base station and not the onward link from there to "the Internet". His comments about the rather heavy handed nature of the test are very relevant; it's a bit hard on Google, too! Where do you actually need Internet connectivity to? How frequent are control inputs over the Internet necessary? How serious is the consequence of the robot being uncontrolled for a while? What if the computer has Internet activity but the remote controlling device does not? How about disabling the robot if there is more than a specified time without control input? If the connection is so important, how about both ends sending "I'm OK, you're OK" handshaking messages? WHat if the computer goes down? How about the robot doing the connectivity assurance tests with the controlling station and leave the computer out of the picture (it's just another node on the connection)?
The main thing that this script is supposed to do is turn off the motors once it drives out of range of a wireless network. Right now, it will just keep doing what it was last told to do (i.e. drive away until someone chases it down and turns it off manually). I am not too worried about the controlling computer losing connectivity, and even if it does, the robot is never driven without someone near by. We drive it on an ad-hoc network a lot of the time anyway. I agree, a handshake would be a good idea. In fact, I don't really need to know if the robot can connect to the internet, I just need to know if it can connect to the controlling computer. I could have the script ask the user for the remote computer's IP address and see if that connection is up or perform a handshake. What would be the best way to do this without eating up system resources?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.