Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

ssh executions on list of remote servers on linux and check status of executions

HI, can someone help with this
I would like to have a logic in shell script something like below. Could you provide a sample script

1.      Will read a comma separated string which contains remote server list server1, server2, server3,server4. (store it in a variable)
2.      Need to execute a shell script on remote server in async (sort of fire and forget). Remote script execution might take 15 minutes to complete. (So guess need FOR loop with execute  SSH user@servern “remote_file.sh” & in background mode?)
3.      Capture the PID to a variable for each remote execution.
4.      Check the status of each PID in frequent internal. ( 10 seconds wait and then re-check)
5.      Whichever server/PID finishes, I shouldn’t check the status again for this PID (optional)
6.      Wait for a total of 20 minutes or 10 iterations, then exist from this check
7.      And report success or failure status.

What I’m trying to achieve is, to minimize the total task execution time

So far I’ve tried like this. But I’m sure there is better way to handle it very

thanks in advance
test.sh
Avatar of ahoffmann
ahoffmann
Flag of Germany image

> 1. .. 3. 7.

servers="server1, server2, server3,server4"
echo $servers|awk -F, '{for(i=1;i<=NF;i++){print "ssh user@"$i" your-command && echo success||echo failure &"}}'|sh

> 4.
jobs

> 5.
echo 'useless as there is no job if finished'
Avatar of simon3270
The command:
    ssh user@"$i" your-command && echo success||echo failure
doesn't do what you'd expect.  If the ssh fails (i.e returns a non-zero return code), processing on this line stops.  If the ssh succeeds, the processing moves on to the "echo success".  If the "echo success" succeeds, processing stops.  If the "echo success" fails, it moves on to the "echo failure" command.  Since "echo" rarely fails, you will almost never get to the "echo failure" command.  You certainly won't get to it if the ssh fails.
SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My bad, I'd done my testing with more general commands.

The problem happens if the first command succeeds and the second command returns an error - in that case, the second *and* the third commands are executed.  So the simple case (where second and third commands succeed) works, but any change which might allow failure in that second command will be a disaster.
Avatar of enthuguy

ASKER

Thanks guys,

sorry for the delay.