Pergunta

im working on distributed systems and my program is complete, i have tested it and it runs fine over 10 machines, but every time i want to test the program i have to: - Copy the file for each machine - ssh to each machine and type "java -jar file"

To avoid that painfull process I made this

for i in {1..11}
do
    if [ $i -ne 6 ]; 
    then
        sshpass -p "qwerty" scp myJar.jar user@l040101-ws$i.XXXX.XX.XX:someFolder;
        sshpass -p "qwerty" ssh user@l040101-ws$i.XXXX.XX.XX "java -jar someFolder/myJar.jar &" &
    fi
done

And for some reason it doesnt work like it should, the scp command executes as it should, but the other one doesnt.

The program should produce a folder with 2 logs inside and if i do it manually it does, so i guess is not permission problem, but not with the script. The weired thing is if i run top, i can see the java processes running in each machine.

BTW: those 2 & is so it the script doesnt get stuck after running each jar

Foi útil?

Solução

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"

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top