Bash script: switch between two terminals to execute commands












1















I want to have a script that first connects me to a remote pc with ssh and executes some commands there. These commands start a process, which will run forever (stops with user input). Now, I must run some commands on my local pc (unfortunately, it's not possible to run them before ssh'ing). So my thoughts are to open a new terminal before connecting to remote, then run ssh and the commands, and then switch back to the terminal in local and execute the rest of the commands.
The two inputs are the port on the remote and the port on the local.



Till now, my script looks like follow:



#!/bin/bash

echo "Connecting to server"
ssh -t -t <name>@ip << EOF
cd /path/to/directory &&
conda activate <environment_name> &&
tensorboard --logdir=<log/directory> --port $2
EOF
echo "Connected"

gnome-terminal -e exit
echo "Connecting with localhost"
ssh -N -f -L localhost:$1:localhost:$2 <name>@ip


But this does not work. I only get connected to the remote, and it's not possible to open a xterm afterwards, because tensorboard is constantly running. So, how to open a terminal beforehand and switch back to it?



Or any other suggestion how to solve this issue?



Many thanks in advance!










share|improve this question























  • I'd use tmux or screen.

    – glenn jackman
    1 hour ago
















1















I want to have a script that first connects me to a remote pc with ssh and executes some commands there. These commands start a process, which will run forever (stops with user input). Now, I must run some commands on my local pc (unfortunately, it's not possible to run them before ssh'ing). So my thoughts are to open a new terminal before connecting to remote, then run ssh and the commands, and then switch back to the terminal in local and execute the rest of the commands.
The two inputs are the port on the remote and the port on the local.



Till now, my script looks like follow:



#!/bin/bash

echo "Connecting to server"
ssh -t -t <name>@ip << EOF
cd /path/to/directory &&
conda activate <environment_name> &&
tensorboard --logdir=<log/directory> --port $2
EOF
echo "Connected"

gnome-terminal -e exit
echo "Connecting with localhost"
ssh -N -f -L localhost:$1:localhost:$2 <name>@ip


But this does not work. I only get connected to the remote, and it's not possible to open a xterm afterwards, because tensorboard is constantly running. So, how to open a terminal beforehand and switch back to it?



Or any other suggestion how to solve this issue?



Many thanks in advance!










share|improve this question























  • I'd use tmux or screen.

    – glenn jackman
    1 hour ago














1












1








1








I want to have a script that first connects me to a remote pc with ssh and executes some commands there. These commands start a process, which will run forever (stops with user input). Now, I must run some commands on my local pc (unfortunately, it's not possible to run them before ssh'ing). So my thoughts are to open a new terminal before connecting to remote, then run ssh and the commands, and then switch back to the terminal in local and execute the rest of the commands.
The two inputs are the port on the remote and the port on the local.



Till now, my script looks like follow:



#!/bin/bash

echo "Connecting to server"
ssh -t -t <name>@ip << EOF
cd /path/to/directory &&
conda activate <environment_name> &&
tensorboard --logdir=<log/directory> --port $2
EOF
echo "Connected"

gnome-terminal -e exit
echo "Connecting with localhost"
ssh -N -f -L localhost:$1:localhost:$2 <name>@ip


But this does not work. I only get connected to the remote, and it's not possible to open a xterm afterwards, because tensorboard is constantly running. So, how to open a terminal beforehand and switch back to it?



Or any other suggestion how to solve this issue?



Many thanks in advance!










share|improve this question














I want to have a script that first connects me to a remote pc with ssh and executes some commands there. These commands start a process, which will run forever (stops with user input). Now, I must run some commands on my local pc (unfortunately, it's not possible to run them before ssh'ing). So my thoughts are to open a new terminal before connecting to remote, then run ssh and the commands, and then switch back to the terminal in local and execute the rest of the commands.
The two inputs are the port on the remote and the port on the local.



Till now, my script looks like follow:



#!/bin/bash

echo "Connecting to server"
ssh -t -t <name>@ip << EOF
cd /path/to/directory &&
conda activate <environment_name> &&
tensorboard --logdir=<log/directory> --port $2
EOF
echo "Connected"

gnome-terminal -e exit
echo "Connecting with localhost"
ssh -N -f -L localhost:$1:localhost:$2 <name>@ip


But this does not work. I only get connected to the remote, and it's not possible to open a xterm afterwards, because tensorboard is constantly running. So, how to open a terminal beforehand and switch back to it?



Or any other suggestion how to solve this issue?



Many thanks in advance!







command-line bash scripts ssh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









LJagLJag

82




82













  • I'd use tmux or screen.

    – glenn jackman
    1 hour ago



















  • I'd use tmux or screen.

    – glenn jackman
    1 hour ago

















I'd use tmux or screen.

– glenn jackman
1 hour ago





I'd use tmux or screen.

– glenn jackman
1 hour ago










1 Answer
1






active

oldest

votes


















0














Have you considered running the job in the background?
You can achieve that if you append an ampersand & to the end of the command. You can send multiple jobs to the background bgand then bring them back in the foreground using fg






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1128646%2fbash-script-switch-between-two-terminals-to-execute-commands%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Have you considered running the job in the background?
    You can achieve that if you append an ampersand & to the end of the command. You can send multiple jobs to the background bgand then bring them back in the foreground using fg






    share|improve this answer




























      0














      Have you considered running the job in the background?
      You can achieve that if you append an ampersand & to the end of the command. You can send multiple jobs to the background bgand then bring them back in the foreground using fg






      share|improve this answer


























        0












        0








        0







        Have you considered running the job in the background?
        You can achieve that if you append an ampersand & to the end of the command. You can send multiple jobs to the background bgand then bring them back in the foreground using fg






        share|improve this answer













        Have you considered running the job in the background?
        You can achieve that if you append an ampersand & to the end of the command. You can send multiple jobs to the background bgand then bring them back in the foreground using fg







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        Apostolos AthanasiouApostolos Athanasiou

        364




        364






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1128646%2fbash-script-switch-between-two-terminals-to-execute-commands%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            香粉寮

            GameSpot