Ping Spawning Out of Control - linux

I have a simple script (below), which needs to spawn 2 ping processes in the background, which will ping 2 hosts continuously.
Without the 'sleep 3' statement, the script spawns hundreds of ping processes until the server locks up. With the sleep statement, the script works properly, only spawning 2 processes.
This issue only occurs on RH5 servers. RH6 seems to work fine, without the sleep statement. My guess on the cause is that the scripts output is being buffered, and is somehow interfering with each other.
My 3 questions are, what is causing the pings to spawn out of control, how exactly does the sleep correct it, and is there a better fix than adding the sleep statement?
Thanks in advance for any info.
Script:
#!/bin/sh
ping 192.168.1.2 -q > /dev/null &
sleep 3
ping 192.168.1.3 -q > /dev/null &
OS Info:
Linux wppra01a0326 2.6.18-402.el5 #1 SMP Thu Jan 8 06:22:34 EST 2015 x86_64 x86_64 x86_64 GNU/Linux

Related

A bash shell program to wait for all servers in a list to start, then run startup scripts

I'm trying to automate the startup (after reboot) of an application that runs on a grid of 12 Linux 7 servers.
The 12 servers get rebooted in random order.
All the servers need to be running before I can start up the application that resides on the 12 servers.
What I'd like to do is to test that all 12 Linux 7 servers are up and then when all 12 are up, I'd proceed with the startup sequence of commands.
All 12 server are set up with ssh keys.
cat serverlist.txt
10.0.0.6
10.0.0.7
10.0.0.8
10.0.0.9
...
10.0.0.18
I want to ping a server and then wait until the ping is successful, then move to the next IP address.
My apologies for my question.
How to code this?
Read in the first line from IP list file.
Ping first IP until success, then ping the other IP addresses (one at a time) until the success of all 12.
Then, run commands to start the application on the grid of 12 servers.
Question: How to code this in the bash shell.
The inner loop can be as simple as
while ! ssh "${connection_string}" -o ConnectTimeout=5 true
do
sleep 0.5
done
This runs a trivial command, and waits for 0.5 seconds between retries.
for i in `cat /home/Startup/serverlist.txt`
do
ssh ${i} -o ConnectTimeout=5 true
while test $? -gt 0
do
ssh ${i} -o ConnectTimeout=5 true
done
done
exit
If ssh works the server is alive, plus the use of the timeout is a better way to speed up the script execution. If the script get finish that means all the servers are up and responding.

Bash poweroff script hangs system

My intention is to cycle through my list of ips and poweroff if my ping succeeds first. However the systems seems to hang. After running this script I can't ping the systems anymore and they aren't powered off. If I run ssh 192.168.1.ip "sudo poweroff" through terminal I dont encounter this issue. Any advice?
for ((ip=40, cnt=0; ip<=max; ip++, cnt++))
do
if ping -c 1 192.168.1.$ip &> /dev/null
then
printf "\n${array[$cnt]}: Ping Successful"
ssh 192.168.1.$ip "sudo poweroff"
printf "\n${array[$cnt]}: Power Down Executed\n"
sleep 1
else
printf "\n${array[$cnt]}: Ping Failed\n"
fi
done
After running a single ssh 192.168.1.40 "sudo poweroff" the system is properly powered off. When running my script, I am unable to ping the systems however I can visually see the fans and leds are still on. I think I should use a KVM to take a closer look since ssh doesn't allow allow connection after this script is run. Still at first glance I dont understand how running ssh 192.168.1.40 "sudo poweroff" and running it through my script really makes a difference. Anyways I'll try to add more information tomorrow.
ssh 192.168.1.$ip "(sleep 5; sudo poweroff)&" to put the process in the background on the remote host and sleep for 5 seconds before powering off to give time for the script to complete and exit the remote host before it goes down... – David C. Rankin
This Resolved my Issue.

Bash Shell: How can I know if TeamViewer has disconnected?

Sometimes TeamViewer disconnects itself (or gets disconnected) from its internet's main servers.
I am programming a script that will check if connection is lost and, if yes, kills and reopens the concerned process to make TeamViewer up and running again.
The problem is: I don't know how to discover that TeamViewer has lost its remote access capability (this is: the capability to be remotely accessed and controlled).
Tested until now:
Check TeamViewer process and/or daemon. Not valid: they keep working even after disconnected.
NICs review. Not valid: TeamViewer seems not to add any.
See the TeamViewer's main window. Not programmatically valid or easy to implement.
How can I programmatically know if TeamViewer has disconnected?
I don't know if this method differs between platforms, but at least I would like to know about a solution for some Linux shell. Bash if possible.
Probably I'm late, but run into the same problem and found a possible solution. I'm using teamviewer 12.
I noticed that, in my case sometimes some GUI related process are not launched so the machine is not online in my computer and contact list, if I ssh it and check for the list of teamviewer processes using:
ps -ef | grep [t]eamviewer
I get just one process, the teamviewer daemon:
root 1808 1 0 09:22 ? 00:00:53 /opt/teamviewer/tv_bin/teamviewerd -d
But, when everything is fine I have:
root 1808 1 0 09:22 ? 00:00:53 /opt/teamviewer/tv_bin/teamviewerd -d
rocco 10975 8713 0 09:31 ? 00:00:58 /opt/teamviewer/tv_bin/wine/bin/wineserver
rocco 11064 10859 0 09:31 ? 00:00:33 /opt/teamviewer//tv_bin/TVGuiSlave.64 31 1
rocco 11065 10859 0 09:31 ? 00:00:28 /opt/teamviewer//tv_bin/TVGuiDelegate 31 1
So simply counting the number of process works for me..
#!/bin/bash
online() {
## Test connection
ping -c1 www.google.com > /dev/null
return $?
}
online
if (test $? -eq 0)
then
network=$(ps -ef | grep [t]eamviewer | wc -l)
if (test $network -gt 3)
then
echo Machine online, teamviewer connected
else
echo Machine online, teamviewer not connected, trying restart daemon
sudo teamviewer --daemon restart
fi
fi
Have you considered trapping the signal(if possible) and executing a function that will restart TeamViewer.
Start it from a script and trap an exit signal
function restartTV {
# re-start TeamViewrt
sudo /etc/init.d/something start
}
trap finish EXIT # or appropriate signal
sudo /etc/init.d/something stop
# Do the work...

How to save "mtr --report-wide" output to textfile?

I am having troubles saving the output of "mtr --report-wide" to a textfile. Probably due to the different way both options output their information. I know i could use the "--raw" argument but i would like to avoid that.
Does anybody have a solution?
Linux version:
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.63-2+deb7u1 x86_64 GNU/Linux
Works:
"nohup mtr --report destination --report-cycles=10 > output &"
Does not work(process never stops):
"nohup mtr --report-wide destination --report-cycles=10 > output &"
process never stops
Quite the contrary - the process is stopped immediately due to a SIGTTOU signal, and thus never terminates.
solution?
Just redirect STDERR also by using … >&output& instead of … >output&.

Parent process that repeatedly forks off short-lived children consumes more and more CPU over time

I have a bash script that does the following things in a loop:
sleep 0.04 seconds
ping another host on the same LAN with count=1 (only one echo request sent) and timeout limit of 1 second if host does not respond.
The problem is that the script takes ~1% CPU at the time I invoked it and over time, let's say after 2-3 hours, I see the same script consumes ~10% CPU and slightly consumes more and more CPU over time.
I use this script in two environments:
VMware host where there is only one CPU/core.
Real machine with one quad-core CPU (Intel i7).
Both with Linux kernel v2.6.32 on x86.
Now, I also notice that if the host I try to ping does not respond (so ping returns after timeout of 1 second), the problem does not occur.
Problem occurs only if host on the LAN is responsive and ping returns quickly after echo reply it gets.
using VMWare, I don't observe the problem at all. no matter if the other host is responsive or not. therefore, I assume that it is actually related to a multi-core environment that matters.
Also, 'strace -c' shows that the bash script is 99% of its time in waitpid() - which means that it sleeps and waiting for its child processes (sleep & ping) to exit.
#!/bin/bash
if [ "$1" == "" ]; then
echo -e "\nUsage: $0 <target host>\n"
exit 1
fi
TARGET_HOST=$1
while (true); do
sleep 0.04
ping $TARGET_HOST -W 1 -c 1 > /dev/null 2>&1
done

Resources