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:


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

  • 1
    I'd use tmux or screen. – glenn jackman Mar 25 '19 at 21:11

I would recommend byobu. I hope the following code server your purpose:

# Check if there is already a session named 'remote'
if [ -z "$(byobu list-sessions | grep remote)" ]
    # If not, start a new session 
    byobu new-window -n remote ssh user@remote;
    byobu send-keys -t remote 'conda activate <env>' 'C-m'
    byobu send-keys -t remote 'tensorboard --logdir=<log/directory> --port $2' 'C-m'
    byobu new-window -n local;
    byobu send-keys -t local 'ssh -N -f -L localhost:$1:localhost:$2 <name>@ip' 'C-m'
# Attach session
byobu attach-session -t remote

In the code above, I am not sure what are $1 and $2! I would recommend creating a small function accessible (either in .bashrc or an executable in $HOME/bin), which would accept these parameters...

Hope this helps!

-- Mike


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.