1

I have a bash function which checks if there are any process running on servers by reading their IPs from a text file one by one

  while read IP
  do
    if [ 1 -eq "$(echo "$(ssh -n ubuntu@$IP "top -b -n2 -d 0.5|grep Cpu|awk '{print \$2+\$4}'|tail -n1") > 1.0" | bc)" ];then
      echo "process is running on $IP"
    else
      echo "process is not running on $IP"
    fi 
  done < file.ips

file.ips contains few server ips

202.X.X.X
203.X.X.X
204.X.X.X
...
...
...

I want to modify this function to check the process running on multiple servers parallelly

2 Answers 2

1

With GNU Parallel you can do:

check() {
  IP="$1"
  if [ 1 -eq "$(echo "$(ssh -n ubuntu@$IP "top -b -n2 -d 0.5|grep Cpu|awk '{print \$2+\$4}'|tail -n1") > 1.0" | bc)" ];then
    echo "process is running on $IP"
  else
    echo "process is not running on $IP"
  fi
}
export -f check
parallel -j0 check < file.ips
0

What about running the content of the loop in the background?

  while read IP
  do
    (if [ 1 -eq "$(echo "$(ssh -n ubuntu@$IP "top -b -n2 -d 0.5|grep Cpu|awk '{print \$2+\$4}'|tail -n1") > 1.0" | bc)" ];then
      echo "process is running on $IP"
    else
      echo "process is not running on $IP"
    fi) & 
  done < file.ips

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.