Run many Java applications via ssh in bash scripts… here is a solution to the problem.
Run many Java applications via ssh in bash scripts
I’m trying to optimize some experiments with Java applications. The same application on many machines. I want to run 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 [email protected] "java java-app.jar" > /data/some-logs.log
((COUNTER++))
((IP_BEGINS++))
done
However, when I run the script, I have to wait a few moments and then press Ctrl+C on each machine. How do I run each app in the background?
Solution
Prefix with nohup
and append &
after the command, which will run it in the background.
while [ $COUNTER -lt $WORKERS ]
do
ssh [email protected] "nohup java -jar java-app.jar > /data/some-logs.log 2>&1 &"
((COUNTER++))
((IP_BEGINS++))
done
You may need to handle the quotation marks
and position of the & to ensure that remote ssh commands are in the background and not local ssh
Edit – I revised the answer based on your comment. Stderr redirection is also added to the same log file, which may help if something goes wrong