How to write in bash to ssh to different machine and create tmux session then run some command in it

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












Basically I want to write some script like



#!/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


which can do the thing separately as:



ssh machine$idx 

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

python run.py


but after many trial and error, I still cannot make it work as expected.



UPDATE following Tagwint's advice, my script is:



#!/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


but it prompts:



./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


UPDATE I modify it to be



#!/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


this works, but after run the script, I log into machine36 and machine37, go into the opened myTempSession, the python run.py is not executed







share|improve this question

























    up vote
    0
    down vote

    favorite












    Basically I want to write some script like



    #!/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


    which can do the thing separately as:



    ssh machine$idx 

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

    python run.py


    but after many trial and error, I still cannot make it work as expected.



    UPDATE following Tagwint's advice, my script is:



    #!/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


    but it prompts:



    ./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


    UPDATE I modify it to be



    #!/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


    this works, but after run the script, I log into machine36 and machine37, go into the opened myTempSession, the python run.py is not executed







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Basically I want to write some script like



      #!/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


      which can do the thing separately as:



      ssh machine$idx 

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

      python run.py


      but after many trial and error, I still cannot make it work as expected.



      UPDATE following Tagwint's advice, my script is:



      #!/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


      but it prompts:



      ./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


      UPDATE I modify it to be



      #!/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


      this works, but after run the script, I log into machine36 and machine37, go into the opened myTempSession, the python run.py is not executed







      share|improve this question













      Basically I want to write some script like



      #!/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


      which can do the thing separately as:



      ssh machine$idx 

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

      python run.py


      but after many trial and error, I still cannot make it work as expected.



      UPDATE following Tagwint's advice, my script is:



      #!/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


      but it prompts:



      ./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


      UPDATE I modify it to be



      #!/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


      this works, but after run the script, I log into machine36 and machine37, go into the opened myTempSession, the python run.py is not executed









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 28 at 11:58
























      asked Jun 28 at 9:52









      K.Wanter

      1204




      1204




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I'd suggest you HERE-DOC apporach



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


          Note $idx part in session name will most probably resolve in nothing
          unless you have a idx environment variable defined
          so you'll get session name just myTempSession






          share|improve this answer























          • thank you, @Tagwint pls see my update, there is maybe some typos
            – K.Wanter
            Jun 28 at 11:40











          • HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
            – Tagwint
            Jun 28 at 12:35










          • I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
            – Tagwint
            Jun 28 at 12:41











          • I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
            – K.Wanter
            Jun 29 at 5:15










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452394%2fhow-to-write-in-bash-to-ssh-to-different-machine-and-create-tmux-session-then-ru%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I'd suggest you HERE-DOC apporach



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


          Note $idx part in session name will most probably resolve in nothing
          unless you have a idx environment variable defined
          so you'll get session name just myTempSession






          share|improve this answer























          • thank you, @Tagwint pls see my update, there is maybe some typos
            – K.Wanter
            Jun 28 at 11:40











          • HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
            – Tagwint
            Jun 28 at 12:35










          • I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
            – Tagwint
            Jun 28 at 12:41











          • I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
            – K.Wanter
            Jun 29 at 5:15














          up vote
          0
          down vote



          accepted










          I'd suggest you HERE-DOC apporach



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


          Note $idx part in session name will most probably resolve in nothing
          unless you have a idx environment variable defined
          so you'll get session name just myTempSession






          share|improve this answer























          • thank you, @Tagwint pls see my update, there is maybe some typos
            – K.Wanter
            Jun 28 at 11:40











          • HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
            – Tagwint
            Jun 28 at 12:35










          • I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
            – Tagwint
            Jun 28 at 12:41











          • I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
            – K.Wanter
            Jun 29 at 5:15












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I'd suggest you HERE-DOC apporach



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


          Note $idx part in session name will most probably resolve in nothing
          unless you have a idx environment variable defined
          so you'll get session name just myTempSession






          share|improve this answer















          I'd suggest you HERE-DOC apporach



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


          Note $idx part in session name will most probably resolve in nothing
          unless you have a idx environment variable defined
          so you'll get session name just myTempSession







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 28 at 12:42


























          answered Jun 28 at 11:20









          Tagwint

          1,3201612




          1,3201612











          • thank you, @Tagwint pls see my update, there is maybe some typos
            – K.Wanter
            Jun 28 at 11:40











          • HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
            – Tagwint
            Jun 28 at 12:35










          • I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
            – Tagwint
            Jun 28 at 12:41











          • I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
            – K.Wanter
            Jun 29 at 5:15
















          • thank you, @Tagwint pls see my update, there is maybe some typos
            – K.Wanter
            Jun 28 at 11:40











          • HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
            – Tagwint
            Jun 28 at 12:35










          • I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
            – Tagwint
            Jun 28 at 12:41











          • I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
            – K.Wanter
            Jun 29 at 5:15















          thank you, @Tagwint pls see my update, there is maybe some typos
          – K.Wanter
          Jun 28 at 11:40





          thank you, @Tagwint pls see my update, there is maybe some typos
          – K.Wanter
          Jun 28 at 11:40













          HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
          – Tagwint
          Jun 28 at 12:35




          HERE-DOC is picky about indentations, so you did it right. I dont know how python run.py should look like to prove it is executed Any errors shown? Either python or run.py may just not be visible. My first guess is - Try adding /full/path/to/python and /full/path/toyour/run,py
          – Tagwint
          Jun 28 at 12:35












          I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
          – Tagwint
          Jun 28 at 12:41





          I guess in the send-keys -t -s "myTempSession" line you should remove -s. Make it look send-keys -t "myTempSession" ...... that's copy/paste typo ), I'll update my answer above
          – Tagwint
          Jun 28 at 12:41













          I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
          – K.Wanter
          Jun 29 at 5:15




          I finally make it work as : tmux new-session -d -s "launch_train" ; send-keys "python run.py
          – K.Wanter
          Jun 29 at 5:15












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452394%2fhow-to-write-in-bash-to-ssh-to-different-machine-and-create-tmux-session-then-ru%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)