Bash script: switch between two terminals to execute commands
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
add a comment |
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
I'd use tmux or screen.
– glenn jackman
1 hour ago
add a comment |
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
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
command-line bash scripts ssh
asked 2 hours ago
LJagLJag
82
82
I'd use tmux or screen.
– glenn jackman
1 hour ago
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered 1 hour ago
Apostolos AthanasiouApostolos Athanasiou
364
364
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I'd use tmux or screen.
– glenn jackman
1 hour ago