bash에서 ssh를 다른 시스템에 쓰고 tmux 세션을 만든 다음 그 안에서 몇 가지 명령을 실행하는 방법

bash에서 ssh를 다른 시스템에 쓰고 tmux 세션을 만든 다음 그 안에서 몇 가지 명령을 실행하는 방법

기본적으로 다음과 같은 스크립트를 작성하고 싶습니다.

#!/bin/bash
for idx in 1 2 3 4 5 6
do
        echo machine$idx
        ssh machine$idx tmux new-session -d -s "myTempSession$idx" python run.py
done

이 작업은 단독으로 수행할 수 있습니다.

ssh machine$idx 

tmux new-session -d -s "myTempSession$idx"

python run.py

그러나 많은 시행착오를 겪은 후에도 여전히 예상대로 작동하도록 할 수 없습니다.

고쳐 쓰다Tagwint의 제안에 따라 내 스크립트는 다음과 같습니다.

#!/bin/bash
for idx in 1 2 3 4 5 6
do
        ssh machine$idx <<REMSH
        tmux new-session -d -s "myTempSession"
        tmux send-keys -t -s "myTempSession" python Space run.py  C-m
        REMSH
done

그러나 다음과 같은 메시지가 표시됩니다.

./dist_run.sh: line 8: warning: here-document at line 4 delimited by end-of-file (wanted `REMSH')
./dist_run.sh: line 9: syntax error: unexpected end of file

고쳐 쓰다나는 그것을 다음과 같이 바꿨다.

#!/bin/bash
for idx in 36 37
do
        ssh machine$idx <<REMSH
        tmux new-session -d -s "myTempSession"
        tmux send-keys -t -s "myTempSession" python Space run.py C-m
REMSH
done

작동하지만 스크립트를 실행한 후 로그인 machine36하여 machine37열려 있는 myTempSession을 입력했지만 python run.py실행되지 않습니다.

답변1

HERE-DOC 방법을 사용하는 것이 좋습니다.

ssh machine$idx <<REMSH
tmux new-session -d -s "myTempSession$idx"
tmux send-keys -t "myTempSession$idx" python Space run.py  C-m 
REMSH

세션 이름의 $idx 부분은 idx 환경 변수를 정의하지 않는 한 해결되지 않을 가능성이 높습니다. 이 경우 세션 이름 myTempSession을 얻게 됩니다.

관련 정보