LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-08-2009, 10:05 AM   #1
aloishis89
Member
 
Registered: Sep 2007
Posts: 54

Rep: Reputation: 15
shell script help


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.
 
Old 08-08-2009, 10:17 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Works fine for me after changing while [1] to while true.

GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)
 
Old 08-08-2009, 10:41 AM   #3
aloishis89
Member
 
Registered: Sep 2007
Posts: 54

Original Poster
Rep: Reputation: 15
Do you know how I could send stuff over the serial port?
 
Old 08-08-2009, 10:52 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Yes. Have you searched for the technique yet? "LQ helps them as helps themselves"
 
Old 08-08-2009, 11:33 AM   #5
aloishis89
Member
 
Registered: Sep 2007
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 08-08-2009, 12:24 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 08-08-2009, 12:44 PM   #7
aloishis89
Member
 
Registered: Sep 2007
Posts: 54

Original Poster
Rep: Reputation: 15
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?
 
Old 08-08-2009, 01:39 PM   #8
aloishis89
Member
 
Registered: Sep 2007
Posts: 54

Original Poster
Rep: Reputation: 15
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?
 
Old 08-09-2009, 08:52 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by aloishis89 View Post
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)?
 
Old 08-11-2009, 01:46 PM   #10
aloishis89
Member
 
Registered: Sep 2007
Posts: 54

Original Poster
Rep: Reputation: 15
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration