Arya Ray Arya Ray - 1 month ago 5
Linux Question

Script does not execute simultaneously in Linux

I have 10 servers and I access them using jumpbox. I wrote a script for starting a service on each server. When I execute that script it's only run on one server and after pressing

Ctrl+C
it's executed on the next server and so on. I want to run it on all servers without pressing
Ctrl+C
.

This is my script:

#!/bin/bash
for i in `cat /etc/hosts | grep prod | awk '{print $2}'` ;
do
ssh $i "hostname && sudo service name start"
done;

Answer

User & to fork new process.

#!/bin/bash
for i in `cat /etc/hosts | grep prod | awk '{print $2}'` ;
do
ssh $i "hostname && sudo service name start" &
done;
Comments