3

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?

Thanks!

EDIT: my actual use case:

./buildSomethingOnRemoteScreen() && ./fetchTheBuiltOutputAndDoSomethingWithIt()
5
  • Not 100% if this will work but can you try wrapping the ls command in double quotes?
    – ryekayo
    Commented Nov 11, 2015 at 20:43
  • Not. still getting 'Connection to myserver closed'
    – Paschalis
    Commented Nov 11, 2015 at 20:44
  • Possibly a duplicate: stackoverflow.com/questions/7049252/… Commented Nov 11, 2015 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
    Commented Nov 11, 2015 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
    Commented Nov 11, 2015 at 22:05

3 Answers 3

1

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
1

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

1
  • 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
    Commented Nov 11, 2015 at 23:58
0

Example of executing a command on remote screen:

ssh myserver -t screen -S build -X stuff "ls .\n"

-X option passes screen specific commands. One such command is stuff which simply puts (stuffs) the given string in the input buffer of a window. If you omit the newline character the command won't be entered (same as you forgot to press enter). Serial terminals may require \r instead of \n for the command to be entered .

When using -X you need to specify session name with -S if you have multiple running.

When using -X you may preselect window with -p. Otherwise the active one is used.

Using readreg/register and paste commands is viable if the command is stored in a file or a serial terminal is slow and the effect of slowpaste is needed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .