Issue
I need to wrap some code in the here document and run it with the ssh:
for service in ${services[@]}
do
ssh -tt root@${server} service=${service} 'bash -s' << 'ENDSSH'
ps axf | grep "service-${service}" | grep -v grep | awk '{print "kill -9 " $1}' | sh
nohup java -jar service-${service}.jar >> nohup.out 2>&1 &
ENDSSH
done
However the for loop can just do the first run and the ssh seems to catch the stdin and the for
loop hangs there forever. How should I do to let the ssh return immediatly and continue the next for execution?
Solution
First, you can replace this:
ps axf | grep "service-${service}" | grep -v grep | awk '{print "kill -9 " $1}' | sh
With this:
pkill -f "service-${service}"
You can skip the "here document" and simplify:
for service in ${services[@]}
do
{
echo "pkill -f service-${service}";
echo "nohup java -jar service-${service}.jar >> nohup.out 2>&1 &";
} | ssh -tt root@${server}
done
Or if you prefer the heredoc:
for service in ${services[@]}
do
ssh -tt root@${server} <<EOF
pkill -f service-${service}
nohup java -jar service-${service}.jar >> nohup.out 2>&1 &
EOF
done
Answered By - John Zwinck