On a remote server I have multiple screens open and I use each one for different purpose. For example the screen with the ID 'build' it is being used to compile some programs. I can open that screen anytime, using:

 ssh myserver -t screen -r build

However, I 'd like to execute a command on that remote screen, directly from my host machine. For example I 'd like to perform an ls in the remote build screen. I 've tried something like:

 ssh myserver -t screen -r build -X ls .

but without any luck.

Any ideas? Is this even possible?


EDIT: my actual use case:

./buildSomethingOnRemoteScreen() && ./fetchTheBuiltOutputAndDoSomethingWithIt()
  • Not 100% if this will work but can you try wrapping the ls command in double quotes? – ryekayo Nov 11 '15 at 20:43
  • Not. still getting 'Connection to myserver closed' – Paschalis Nov 11 '15 at 20:44
  • Possibly a duplicate: stackoverflow.com/questions/7049252/… – Valentin Bajrami Nov 11 '15 at 20:48
  • If I'm reading man screen correctly, the -X option should only send a command, not reattach. Can you attach to the screen after doing that, and see if it ran the command silently? – Tom Hunt Nov 11 '15 at 21:07
  • It might do actually, but I want to know when something on remote has finished before I continue! I 've updated my question to put my actual use case.. and I think it might not be possible! Thanks though! – Paschalis Nov 11 '15 at 22:05

You can execute any script with paste command:

# read script into register p
ssh hostname screen -S build -X readreg p script_on_the_host
# paste contents of register p into running screen session
ssh hostname screen -S build -X paste p

I use tmux (more flexible than screen) on my server, and to executing a command directly after connecting:

ssh myserver -t 'tmux send-keys -t session-name "ls ./" C-m; tmux attach -t session-name '

You just have to make sure that there is a session running with session-name. The C-m emulates ENTER after typing in "ls ./"

  • I consider to move from screen to tmux or byobu. I will make the switch at some point in the future, and check this out! – Paschalis Nov 11 '15 at 23:58

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.