send command to an already running screen session

11

5

I have been trying to send commands to a running gnu screen session (4.00.03) in opensolaris, but cannot get it to run any commands through any combination of screen -X

Ok, I start a screen session with screen -S test, and then tried to with screen -r -X "date"to just show me the date, when I would reconnect to it. But neither an error message nor output in the screen happened. I tried with so many combinations, that I can't even remember.

Any hints on how to accomplish it?

The reason why I am doing this is, because I have a program, which does not come as a daemon, and I wish to start it in a screen session, so I can later on see what is going on.

aXon

Posted 2010-03-05T08:59:28.613

Reputation:

1it seems, as if this "command" that they are talking about in the man pages is meant for a screen command, like settings for the screen session itself. Rather than sending commands for the shell, which is opened in the screen session. So, anyone an idea of how to make it happen with a bash script?? – None – 2010-03-05T11:29:35.790

@Axon: Yes, -X sends screen commands, not shell commands, but screen has a command 'screen' which can open new screen windows. – None – 2010-03-05T20:43:57.013

Answers

18

Actually it's quite simple. You can use the following:

screen -S sessionname -X stuff 'command'`echo -ne '\015'`

echo -ne '\015' emulates pressing the Enter key.

NOTE: that 'stuff' is a screen command. http://www.gnu.org/software/screen/manual/screen.html#Stuff

thegatekeeper

Posted 2010-03-05T08:59:28.613

Reputation: 189

this doesn't work unless I run the command twice... and on separate command lines... if I do screen -d -m -S sessionname; screen -S sessionname -X [...] nothing will ever work even (screen -d -m -S sessionname&)&sleep 10;screen -S sessionname -X [...] does nothing except create a new screen session that does nothing. down-voting – user3338098 – 2016-06-01T17:34:51.453

this doesn't work for me... does not emulate enter key... – Tanner Strunk – 2018-06-10T05:32:05.757

Could you please specify what "stuff" represents? Is that a name...or command...or other parameters? – Cerin – 2012-08-24T21:37:31.430

It works in fact, but I wouldn't say it's simply anyhow ;) This is something like passing keypressed events and passing newline to the screen. Isn't there just normal screen execute command...? – ducin – 2013-02-08T12:55:44.140

@Cecin: stuff is a screen command to 'stuff' a string into stdin – konrad – 2013-09-17T11:51:24.973

This is very good, but for some reason it doesn't work with "screen -RdS sessionname -X stuff 'ls -l'echo -ne '\015'", why? – e271p314 – 2014-02-12T10:21:31.043

5

Sounds like you want:

$ screen -S test -d -m -c /dev/null -- sh -c 'date; exec $SHELL'

-S test: name this session
-d -m: start screen detached
-c /dev/null: ignore ~/.screenrc (provide a different file or drop this option,
  as needed)
--: end of screen options
sh -c 'date; exec $SHELL': command for screen to run, note that if this command
  was just 'date' then screen would exit immediately.  if you don't want to exit
  screen after your command exits, then you might use:
    $ screen -S test -d -m -c /dev/null -- your command here

Or maybe you just want dtach.

Roger Pate

Posted 2010-03-05T08:59:28.613

Reputation:

This answer worked for me. The key was the -- to end the screen options. – Plazgoth – 2016-07-29T17:05:01.010

Running this results in nothing. No success or error messages, but screen -list shows no sessions are open. – Cerin – 2012-08-24T21:44:07.970

1

screen -S <session_name> -X screen [-t <title>] <shell_command> [<args>]

Open a new window (with title title) in an existing session named session_name, and execute shell_command (with optional arguments args)

Jeremy Kao

Posted 2010-03-05T08:59:28.613

Reputation: 111