I recommend using SSH keys rather than expressing the password in a command (which gets logged and is visible to other users, not to mention its presence in your script). The github ssh key generation docs are pretty good for this (to add, append to the server's ~/.ssh/authorized_keys file).
Once you have generated a key on the client and added its pubkey to the server, you should be able to run:
for i in {1..11}
do
if [ $i -ne 6 ] # skip server six
then
cat myJar.jar |ssh user@l040101-ws$i.XXXX.XX.XX \
"cd someFolder; cat > myJar.jar; java -jar myJar.jar" &
fi
done
Note the single ampersand there, which is outside the quotes (so it is run on your client, not your server). You can't send an SSH session to the background because the parent would be killed.
I wrangled this into one line in order to minimize the number of connections (the first cat
command dumps the file into standard output while the second cat
command writes the standard input (the contents of myJar.jar) to the target location). I wasn't sure if I could just pipe it straight to java (cat myJar.jar |ssh user@host "cd someFolder; java -jar -"
), so I left that alone.
I'm assuming you don't have to run the .jar from the parent of someFolder. It seems simpler to actually be in the target directory. If there's a chance the target directory does not exist, add mkdir -p someFolder;
before the cd
command. The -p
will ensure nothing happens if the directory already exists. If you do have to run it from the parent, remove the cd
command and replace "myJar.jar" with "someFolder/myJar.jar"