16

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.

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311

5 Answers5

19

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

stackdump
  • 103
6

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.

2

Like some of the comments on here, I was also unable to get this command to work at first, on Ubuntu Server 22.04. Here's what actually worked for me:

screen -S <sessionName> -p 0 -X stuff 'command^M'

The -p 0 was what did it to select the first window, despite the stuff command claiming that it operates on "the current window", and the ^M was a nicer alternative form of the echo -ne '\015'.

From: https://raymii.org/s/snippets/Sending_commands_or_input_to_a_screen_session.html

2
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)

0

if you are looking here how to send ctrl+a d to the second screen without conflicts and detaching from the first:

  • After entering the inner screen session, press Ctrl+a (command mode)
  • type literally :escape ^Bb to change first screen sequence to ctrl+b d