Run many java applications via ssh in a bash script

araujo :

I'm trying to optimize some experiments with a Java application. The same application is on many machines. I want to run all of them via a bash script with ssh.

I have a bash script that has a while loop to run the application. Like this

while [ $COUNTER -lt $WORKERS ]
do
  ssh  ubuntu@host "java java-app.jar" > /data/some-logs.log 
  ((COUNTER++))
  ((IP_BEGINS++))
done

However when I run the script I have to wait a moment and press Ctrl+C for every machine. How can I run every aplication on background?

stringy05 :

prefix with nohup and append a & to the command, that will run it in the background.

while [ $COUNTER -lt $WORKERS ]
do
  ssh  ubuntu@host "nohup java -jar java-app.jar > /data/some-logs.log 2>&1 &"
  ((COUNTER++))
  ((IP_BEGINS++))
done

You might need to muck around with the quotes and placements of the & to make sure the remote ssh command gets backgrounded and not your local ssh

EDIT - I fixed the answer based on your comment. Also added the stderr redirect to the same log file, that might help when things go wrong

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=126221&siteId=1