1

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
Akunis
  • 11
  • 3

0 Answers0