simple while loop script to check a connection

Hi,

I am completely new to shell scripting. Basically I am wanting to create a simple while loop script to check if a network connection is available from 'netstat -a' and if its present then output a character to the serial port. I think i am nearly there.. but need some assistance.. this is what i have so far..(ip address an example)..

#!/bin/bash
CONNECTED=$(netstat -a | grep -c 1.1.1.1)
while [echo $CONNECTED -gt 0]; do
  echo -n p >/dev/ttyS1;
  sleep 20;
done

Many thanks

Zippy

You don't need to echo CONNECTED

Now check..

#!/bin/bash
CONNECTED=$(netstat -a | grep -c 1.1.1.1)
while [ "$CONNECTED" -gt 0 ]; do
echo -n p >/dev/ttyS1;
sleep 20;
CONNECTED=$(netstat -a | grep -c 1.1.1.1)#I think you need this also inside the while loop. so it can change the value and reflects it..
done
1 Like

thanks for the quick reply.. on the road right now but will check later

---------- Post updated 10-09-12 at 02:53 AM ---------- Previous update was 10-08-12 at 11:18 AM ----------

works a treat pamu thanks

---------- Post updated at 02:54 AM ---------- Previous update was at 02:53 AM ----------

works a treat thanks...

You don't even need the variable:

while netstat -antu|grep  -q 10.1.1.1; do something; done

The -tu options to netstat will restrict its output to tcp/udp sockets only.

Hi I have kind of modified things to change for an application..

#!/bin/bash
CONNECTED=$(ps -ef | grep -c APP1)
while [ "$CONNECTED" -gt 1 ]; do
  echo -n p >/dev/ttyS1;
  echo $CONNECTED
  sleep 10;
  CONNECTED=$(ps -ef | grep -c APP1)
done

one thing I don't know how to get right is exit status.. so when $CONNECTED is less than or equal to 1 I want the loop to continue but not send the output to the serial port. Right now if $CONNECTED is = 1 the script stops..

any help appreciated

Is there any specific requirement for this..?

Please provide details what is purpose behind this so we can try to provide better sollution to you..:slight_smile:

Hi pamu

Thanks for your help so far. Essentially if program A is running i want to continually send serial keepalives to the serial port..(something is scooping this output up fine) if the program fails i want the output to stop.. if the program resumes i want it to continue

Thanks

Zippy

1)Is it possible to edit Program A..? If yes then you can just call your second script from program A.

2)Second way is use crontab for your script that check your Program A stastus after some specific time.

3)Third way you can use your program second in infinite loop.(not good way of programming, but if you wish.)

while true
do
Program second code
done

Hope this helps you..:slight_smile:

Hi Pamu,

Tried the following in /etc/cron.minutely

#!/bin/bash CONNECTED=$(ps -ef | grep -c APP1) while [ "$CONNECTED" -gt 1 ]; do   echo -n p >/dev/ttyS1;   echo $CONNECTED   CONNECTED=$(ps -ef | grep -c APP1) done

it seemed to work but killed the CPU.. ran top and 0% idle.. and lots defunct instances of ps ( see below)..

top - 18:00:58 up  1:32,  1 user,  load average: 114.69, 112.65, 100.98
Tasks: 741 total, 155 running, 554 sleeping,   0 stopped,  32 zombie
Cpu(s): 34.4%us, 64.3%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  1.2%si,  0.0%st
Mem:   2075060k total,   442024k used,  1633036k free,    27356k buffers
Swap:  1052248k total,        0k used,  1052248k free,   205168k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                           
 7549 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7556 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7563 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7566 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7582 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7585 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7597 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7604 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7611 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7614 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7617 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7624 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7627 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7634 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7649 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7652 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7655 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7661 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>                                      
 7665 root      18   0     0    0    0 Z  1.3  0.0   0:00.05 ps <defunct>       

Not sure whats wrong. Do you have any ideas?

Thanks

zippyzip

you don't need while loop now...

just try with if

if [[ $(ps -ef | grep -c APP1) -gt 1]]
then
echo -n p >/dev/ttyS1
fi

that's what you need.