SSH 在 bash heredoc 中的 if 语句中没有正确退出

SSH not exiting properly inside if statement in bash heredoc

所以我运行正在使用这个脚本来检查 java 服务器是否通过 sshing 远程启动。如果它已关闭,我将尝试退出并在本地 运行 另一个脚本。但是退出命令后,还是在远程目录下。

ssh -i ec2-user@$DNS << EOF
    
    if !  lsof -i | grep -q java ; then
        echo "java server stopped running"
        # want to exit ssh
        exit
        # after here when i check it is still in ssh
        # I want to run another script locally in the same directory as the current script
        ./other_script.sh
    else
        echo "java server up"

    fi;
EOF

退出正在退出 ssh 会话,因此永远不会执行 HEREDOC 中的 other_script.sh 行。最好将它放在脚本之外并从 HEREDOC/ssh 的退出状态开始操作,因此:

ssh -i ec2-user@$DNS << EOF

if !  lsof -i | grep -q java ; then
    echo "java server stopped running"
    exit 7   # Set the exit status to a number that isn't standard in case ssh fails
else
    echo "java server up"
fi;
EOF
if [[ $? -eq 7 ]]
then
    ./other_script.sh
fi