When executing remote commands, SSH won't exit until the remote command is complete. Your remote script won't exit until the Java program is complete, and a Java program won't exit until all its non-daemon threads exit, and your Java program runs forever. Therefore, your server-side invocation of SSH runs forever (well, until you kill it through some other means) and your script hangs.
You need to decide on a way to make your SSH remote command return immediately. You have options. The easiest is probably just to invoke it with &
on the server script, as:
ssh -n "$1" "cd $projectDir;./startBatch.sh $2 &"
A more robust option is to invoke java
with &
in the remote script, and let the server-side run as you have it now (no &
), that way you have a chance to completely read e.g. error messages produced by the remote script.
Side Note: As for the password itself (which you will ultimately have to deal with once you get past the current hurdle), as mentioned in my comment on the question: One possibility is to create a passwordless key (ssh-keygen -t rsa
) on your machine then stick the public key in the authorized_keys2
on each of the remote machines, then you won't have to deal with the passwords when connecting from your machine. The SSH password prompt tends to wreak havoc on script interactivity sometimes. Comes with associated security pitfalls, but they may not matter for your situation.
Responding to comments below. You have a couple of options. If you want to capture everything to the same log file, with append, don't redirect your program outputs, and just redirect everything the while loop does to a log, e.g.:
while [ "$procNumber" -lt "$batchSize" ]; do
procName="$batchNumber"_"$procNumber"
exec -a jnode_"$procName" java -cp build/classes runner.Runner "$procName.txt" &
procNumber=$(($procNumber + 1))
done >> "$myLog" 2>&1
If you want one log per process, with append:
while [ "$procNumber" -lt "$batchSize" ]; do
procName="$batchNumber"_"$procNumber"
exec -a jnode_"$procName" java -cp build/classes runner.Runner "$procName.txt" >> "$myLog.$procNumber" 2>&1 &
procNumber=$(($procNumber + 1))
done
You could also combine the above two, if you want to separate application output from the output of other commands in the loop.